Sort List Data by Condition by LINQ in .NET Core Using C#.Net

A simple tutorial with example for how to short list data by condition using linq with c#.net. Standard Query Expression to write conditional query.
In today's article I will show you a simple tutorial with example to how to short the list data by condition with LINQ in .NET core using c#.net. This code you can use in your asp.net core mvc, class library, console application and windows application.

In this tutorial we will use the console application to demonstrate the example. Now let's add a class file and below property.  

 public class EmployeeModel
{
    public int Id { get; set; }
    public string Name { get; set; }
    public string Address { get; set; }
    public string Email { get; set; }
} 
In above class I have declare the properties. We will use this property to create the list.  
 List<EmployeeModel> employees = new List<EmployeeModel>() {
     new EmployeeModel { Id=1,Name="John" ,Address="Address10",Email="john@mail.com"},
     new EmployeeModel { Id=2,Name="Mate" ,Address="Address27",Email="mate@mail.com"},
     new EmployeeModel { Id=4,Name="Maru" ,Address="Address10",Email="maru@mail.com"},
     new EmployeeModel { Id=3,Name="Rose" ,Address="Address31",Email="rose@mail.com"},
     new EmployeeModel { Id=5,Name="Krishna" ,Address="Address33",Email="krishna@mail.com"},
     new EmployeeModel { Id=8,Name="Pape" ,Address="Address24",Email="pape@mail.com"},
     new EmployeeModel { Id=10,Name="Pranav" ,Address="Address77",Email="pranav@mail.com"}
}; 
In above list I have added some values, and we will use this list to perform the shorting operation. 
 List<EmployeeModel> employees = new List<EmployeeModel>() {
     new EmployeeModel { Id=1,Name="John" ,Address="Address10",Email="john@mail.com"},
     new EmployeeModel { Id=2,Name="Mate" ,Address="Address27",Email="mate@mail.com"},
     new EmployeeModel { Id=4,Name="Maru" ,Address="Address10",Email="maru@mail.com"},
     new EmployeeModel { Id=3,Name="Rose" ,Address="Address31",Email="rose@mail.com"},
     new EmployeeModel { Id=5,Name="Krishna" ,Address="Address33",Email="krishna@mail.com"},
     new EmployeeModel { Id=8,Name="Pape" ,Address="Address24",Email="pape@mail.com"},
     new EmployeeModel { Id=10,Name="Pranav" ,Address="Address77",Email="pranav@mail.com"}
};
//1= Id, 2=Name 3=Address 4=Email
Console.WriteLine("--CHOOSE YOUR OPTION--");
Console.WriteLine("1. Id\n2. Name\n3. Address\n4.Email");
Console.WriteLine("Enter Your Option: ");
string optionno = Console.ReadLine();
var data = from d in employees
           select d;
if (optionno == "1")
{
    data = data.OrderBy(m => m.Id);
}
if (optionno == "2")
{
    data = data.OrderBy(m => m.Name);
}
if (optionno == "3")
{
    data = data.OrderBy(m => m.Address);
}
if (optionno == "4")
{
    data = data.OrderBy(m => m.Email);
}
data = data.ToList();
foreach (var item in data)
{
    Console.WriteLine(string.Format("Id: {0} , Name:{1} , Address:{2} , Email Id:{3}", item.Id, item.Name, item.Address, item.Email));
} 
In above code I have taken the list of Employee and after that asked user for his input. As per provided input I am shorting the data. Here 1= Id, 2=Name 3=Address 4=Email have been used for shorting the data. 

Just check the LINQ query here i have used Standard Query Expression to get data from the list. So, in you are using Standard Query Expression on that you need to apply list for getting the list item. So here we will not get data till we move to the last step of the code where we have used ToList().

Here in this example as per user input, I am adding the order by condition. Now let's run the code and check the output.

User Input in Console Application

Now let's enter option as 1 to short the data by Id. Here in we can clearly see that the list in got short by Id. 

Short By Id List Using LINQ C#.Net

Now let's enter the option 2 to short the list data by name.

Short List By Name Using LINQ

Post a Comment