USING LINQ to SQL utility.(With and witout Lambda Expression)
To use Linq on the sql database, .net is required to have a map the database tables/stored procedures to Linq SQL classes.Right click on the solution
to and click on add new item, Select LINQ TO SQL classes , We will get a .dbml file file nae: linq_Sql created.From the server explorere panel you can drag and drop a table in the .dbml file
created. The .cs file of the .dbml file willl contain the filenameDatacontext class whcih is used to get the tables present there.
class Program
{
static void Main(string[] args)
{
linq_SqlDataContext mydb = new linq_SqlDataContext();
var employees = from name in mydb.Employee_Tables
where name.Age > 25
&& name.Salary > 12000
select name.Name;
Console.WriteLine("List of People from Database with Linq Query ");
foreach (string n in employees)
{
Console.WriteLine(n);
}
//USING LINQ WITHOUT SQL STATEMENTS
linq_SqlDataContext mydb2 = new linq_SqlDataContext();
IEnumerable<Employee_Table> tb = mydb2.Employee_Tables.Where(u => u.Age > 25 && u.Salary > 12000);
List<Employee_Table> lst = mydb2.Employee_Tables.Where(u => u.Age > 25 && u.Salary > 12000).ToList();
Console.WriteLine("List of People from Database with Linq Query with IEnumerable ");
foreach (var n in tb)
{
Console.WriteLine(n.Name);
}
Console.WriteLine("List of Peopl from database with Lamda expression without IEnumerable");
foreach(var n in lst)
{
Console.WriteLine(n.Name);
}
Console.ReadLine();
}
}
to and click on add new item, Select LINQ TO SQL classes , We will get a .dbml file file nae: linq_Sql created.From the server explorere panel you can drag and drop a table in the .dbml file
created. The .cs file of the .dbml file willl contain the filenameDatacontext class whcih is used to get the tables present there.
class Program
{
static void Main(string[] args)
{
linq_SqlDataContext mydb = new linq_SqlDataContext();
var employees = from name in mydb.Employee_Tables
where name.Age > 25
&& name.Salary > 12000
select name.Name;
Console.WriteLine("List of People from Database with Linq Query ");
foreach (string n in employees)
{
Console.WriteLine(n);
}
//USING LINQ WITHOUT SQL STATEMENTS
linq_SqlDataContext mydb2 = new linq_SqlDataContext();
IEnumerable<Employee_Table> tb = mydb2.Employee_Tables.Where(u => u.Age > 25 && u.Salary > 12000);
List<Employee_Table> lst = mydb2.Employee_Tables.Where(u => u.Age > 25 && u.Salary > 12000).ToList();
Console.WriteLine("List of People from Database with Linq Query with IEnumerable ");
foreach (var n in tb)
{
Console.WriteLine(n.Name);
}
Console.WriteLine("List of Peopl from database with Lamda expression without IEnumerable");
foreach(var n in lst)
{
Console.WriteLine(n.Name);
}
Console.ReadLine();
}
}
Comments
Post a Comment