Sort List and Array in LINQ in .NET Core Using C#.Net

How to short list & array using LINQ with lambda expression, standard SQL query expression in ascending or descending order in .net core and c#.net.
In today's article I will show you various ways to write a LINQ query in .net core using c#.net for shorting List and Array in .net core using c#.net. 

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

Here we will demonstrate how we can short a list by using linq in c#.net against a specific field in ascending. Here we are going to user field "Roll No". We can short a List in ascending order by using OrderBy().

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. 

Short list in ascending order by LINQ in c#.net

Ascending Order by Using Multiple Field

Here we will demonstrate how we can short a list by using LINQ in c#.net by using multiple fields. Here we are going to user field "Roll No" and "Name". We can short a List in ascending order by using from, OrderBy().

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. 

Short List in Ascending Order by Multiple Field in LINQ

Descending Order for a Specific Field

Here we will demonstrate how we can short a list by using linq in c#.net against a specific field in descending order. Here we are going to user field "Roll No". We can short a List in descending order by using OrderByDescending().

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. 

Short List Descending Order for a Specific Field

Descending Order by Using Multiple Field

Here we will demonstrate how we can short a list by using LINQ in c#.net by using multiple fields. Here we are going to user field "Roll No" and "Name". We can short a List in descending order by using from, OrderByDescending().

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. 

Descending Order by Using Multiple Field

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 Ascending Order

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. 

Shot in Descending Order

Post a Comment