Various Way to Write LINQ Query in C#.Net

What are the various ways to write LINQ query in C#.net. Lambda expression and Standard Query Expression are two ways to write LINQ Query.
In today's article I will show what are the different ways to write a LINQ query in .net core using c#.net. Here I will use a console application to perform the example. LINQ provide a way write the code faster and effective manner. There is two ways by which we can write a LINQ query. 

  • 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));
}

In above code I have taken a list of students. In this example I will use lambada expression in with where clause to filter the data on a specific condition. Now let's check the output. 

Expression lambda in LINQ in C#.Net

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>;

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.

Standard Query Expression in LINQ using C#

Post a Comment