- Lambda expression
- Standard Query Expression
Lambda Expression
Lambda expression in c# is used to define as an anonymous function. Lambda expression is declared with =>. Lambda expression help us to perform the query operation same we do in SQL on an object. Lambda expression act as a separator between parameter and the body.
Syntex
(input parameters) => (lambda body)
There are two types of lambda expression.
- Expression lambda
- Statement lambda
Example:
List<StudentMaster> studentMasters = new List<StudentMaster>();
studentMasters.Add(new StudentMaster { RollNo = 1, Name = "Dhirag", Email = "dh@xyz.com", Grade = 8, TotalMarks = 67 });
studentMasters.Add(new StudentMaster { RollNo = 2, Name = "Rakesh", Email = "rk@xyz.com", Grade = 7, TotalMarks = 77 });
studentMasters.Add(new StudentMaster { RollNo = 3, Name = "Prakash", Email = "pr@xyz.com", Grade = 7, TotalMarks = 87 });
studentMasters.Add(new StudentMaster { RollNo = 4, Name = "Chirag", Email = "ch@xyz.com", Grade = 10, TotalMarks = 56 });
studentMasters.Add(new StudentMaster { RollNo = 5, Name = "Suraj", Email = "su@xyz.com", Grade = 7, TotalMarks = 76 });
//Filter the student list with lambda expression
var data = studentMasters.Where(m => m.Grade == 7);
foreach (var item in data)
{
Console.WriteLine(string.Format("Roll No:{0}, Name:{1}, Email:{2}", item.RollNo, item.Name, item.Email));
}
Standard Query Expression
LINQ provide facility to write us the code the way we write in the SQL. Here in this we will use select, from, where, groupby,into, orderby, joins wo write thee LINQ query.
Syntex from <rangevariable> in <collection>
select <rangevariable>;
select <rangevariable>;
Example:
List<StudentMaster> studentMasters = new List<StudentMaster>();
studentMasters.Add(new StudentMaster { RollNo = 1, Name = "Dhirag", Email = "dh@xyz.com", Grade = 8, TotalMarks = 67 });
studentMasters.Add(new StudentMaster { RollNo = 2, Name = "Rakesh", Email = "rk@xyz.com", Grade = 7, TotalMarks = 77 });
studentMasters.Add(new StudentMaster { RollNo = 3, Name = "Prakash", Email = "pr@xyz.com", Grade = 7, TotalMarks = 87 });
studentMasters.Add(new StudentMaster { RollNo = 4, Name = "Chirag", Email = "ch@xyz.com", Grade = 10, TotalMarks = 56 });
studentMasters.Add(new StudentMaster { RollNo = 5, Name = "Suraj", Email = "su@xyz.com", Grade = 7, TotalMarks = 76 });
//All records
var data = from d in studentMasters
select d;
Console.WriteLine("EXAMPLE 1: ALL RECORDS");
foreach (var item in data)
{
Console.WriteLine(string.Format("Roll No:{0}, Name:{1}, Email:{2}", item.RollNo, item.Name, item.Email));
}
//All records
var data1 = from d in studentMasters
where d.Grade == 7
select d;
Console.WriteLine("\nEXAMPLE 2: WITH CONDITION RECORDS");
foreach (var item in data1)
{
Console.WriteLine(string.Format("Roll No:{0}, Name:{1}, Email:{2}", item.RollNo, item.Name, item.Email));
}
In above code I have used standard query operation to for getting all the records and filtering the record with where clause and get the records. now let's run the code to check the output.