What are the various ways to write LINQ query in c#.net?
There are two ways by which we can write a LINQ query. First way to user the lambda expression and other one is using way we use the SQL query by using select, from, where, groupby,into, orderby, joins. So they are:
- Lambda expression
- Standard SQL query like expression
Here I will cover both the ways lambda and query expression to demonstrate the example. So first we will create a new .net core console application. First, we will perform shorting operation with List and after that will do with user Array.
For this article we will use .net core console application to demonstrate the expression.
LINQ Operations with List C#.Net?
After creating the console application, we will add a class file in this we will add some properties to store the data as a list.
public class StudentMaster
{
public int RollNo { get; set; }
public string Name { get; set; }
public string Email { get; set; }
public int Grade { get; set; }
public int TotalMarks { get; set; }
}
Now let's prepare a list for the class code and add some data in it.
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 = 4, 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 = 12, TotalMarks = 76 });
In above list I have added few records. On this record we will perform different operations. Ascending Order for a Specific Field
By Using Lambda expression
var data = studentMasters.OrderBy(m => m.RollNo);
foreach (var item in studentMasters)
{
Console.WriteLine(string.Format("Roll No:{0}, Name:{1}, Email:{2}", item.RollNo, item.Name, item.Email));
}
By Using SQL Query Like Expression
var data = from d in studentMasters
orderby d.RollNo
select d;
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 used OrderBy to short the list in ascending order. Now let's run the code to check the output. In both the above code will return the list date shot in ascending order. Ascending Order by Using Multiple Field
By Using Lambda expression
var data = studentMasters.OrderBy(m => m.RollNo).OrderBy(m => m.Name);
foreach (var item in studentMasters)
{
Console.WriteLine(string.Format("Roll No:{0}, Name:{1}, Email:{2}", item.RollNo, item.Name, item.Email));
}
By Using SQL Query Like Expression
var data = from d in studentMasters
orderby d.RollNo,, d.Name
select d;
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 used OrderBy to short the list in ascending order. Now let's run the code to check the output. In both the above code will return the list date shot in ascending order. Descending Order for a Specific Field
By Using Lambda expression
var data = studentMasters.OrderByDescending(m => m.RollNo);
foreach (var item in studentMasters)
{
Console.WriteLine(string.Format("Roll No:{0}, Name:{1}, Email:{2}", item.RollNo, item.Name, item.Email));
}
By Using SQL Query Like Expression
var data = from d in studentMasters
orderby d.RollNo descending
select d;
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 used OrderByDescending to short the list in descending order. Now let's run the code to check the output. In both the above code will return the list data shot in descending order. Descending Order by Using Multiple Field
By Using Lambda expression
var data = studentMasters.OrderByDescending(m => m.RollNo).OrderBy(m => m.Name);
foreach (var item in studentMasters)
{
Console.WriteLine(string.Format("Roll No:{0}, Name:{1}, Email:{2}", item.RollNo, item.Name, item.Email));
}
By Using SQL Query Like Expression
var data = from d in studentMasters
orderby d.RollNo,, d.Name descending
select d;
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 used OrderByDescending to short the list in descending order. Now let's run the code to check the output. In both the above code will return the list date shot in descending order. LINQ Operations with Array C#.Net?
Now we will write the code to short the array by linq using c#.net. So, for this we will create an Array with few specific values.Shot in Ascending Order
int[] dataArray = { 10, 2, 1, 5, 8 };
var data = dataArray.Order();
foreach (var item in data)
{
Console.WriteLine(string.Format("Value: {0}", item));
}
Let's run the code and check the output. Shot in Descending Order
int[] dataArray = { 10, 2, 1, 5, 8 };
var data = dataArray.OrderDescending();
foreach (var item in data)
{
Console.WriteLine(string.Format("Value: {0}", item));
}
Let's run the code and check the output.