If Condition in LINQ Select Query C#

How to apply if condition in LINQ query to execute conditional LINQ query in C#.net code.
In today's article I will show a simple tutorial with example how you can apply if condition with if else condition in LINQ Select query using c#. We will execute single LINQ select query with different condition using if conditions.  This example you can use in .net core also. So, for this article first we will create new console application. Here we will take a list and having some data and we will apply the condition in same LINQ Select query with multiple if conditions. 

So first we will take the list having some data in it. 

List<EmployeeProperty> employees = new List<EmployeeProperty>()
{
    new EmployeeProperty{ Id=1,Name="Rakesh A", Address="R. Address", RoleId=2},
    new EmployeeProperty{ Id=2,Name="Dhiraj A", Address="D. Address", RoleId=3},
    new EmployeeProperty{ Id=3,Name="Jonhn A", Address="J. Address", RoleId=2},
    new EmployeeProperty{ Id=4,Name="Mat A", Address="M. Address", RoleId=1}
}; 
Here in above code, I have taken a class named as EmployeeProperty and added value in list of EmployeeProperty class. Here we will apply use if condition on role to demonstrate the tutorial example. so here we will use if condition on role. If passed role is zero at that time code will get all user lists and if we are passing RoleId at that time system will return value or record for specific role.  Here we will not use two different query in a single query we will use if condition to get records.

LINQ Code Without IF Condition

List<EmployeeProperty> employees = new List<EmployeeProperty>()
{
    new EmployeeProperty{ Id=1,Name="Rakesh A", Address="R. Address", RoleId=2},
    new EmployeeProperty{ Id=2,Name="Dhiraj A", Address="D. Address", RoleId=3},
    new EmployeeProperty{ Id=3,Name="Jonhn A", Address="J. Address", RoleId=2},
    new EmployeeProperty{ Id=4,Name="Mat A", Address="M. Address", RoleId=1}
};
int roleId = 0;
var query = employees.Where(m => m.Name.Contains("A"));
if (roleId != 0)
{
    query = query?.Where(m => m.RoleId == roleId);
}
query = query.ToList();
foreach (var item in query)
{
    Console.WriteLine($"Id: {item.Id}, Name:{item.Name}, Address:{item.Address}, Role Id={item.RoleId}");
} 
Here in above code I have passed RoleId as zero.  Here in above code i have checked if RoleId is zero on that case it will not apply the condition. Here code will keep adding condition till the time you will not apply .ToList(), Ones you apply ToList() query will execute. Now lets run this code and check the output.

Data without condition

LINQ Code with IF Condition

Now let's make change of RoleId as 2 in code and run the code to check the final output.
List<EmployeeProperty> employees = new List<EmployeeProperty>()
{
    new EmployeeProperty{ Id=1,Name="Rakesh A", Address="R. Address", RoleId=2},
    new EmployeeProperty{ Id=2,Name="Dhiraj A", Address="D. Address", RoleId=3},
    new EmployeeProperty{ Id=3,Name="Jonhn A", Address="J. Address", RoleId=2},
    new EmployeeProperty{ Id=4,Name="Mat A", Address="M. Address", RoleId=1}
};
int roleId = 2;
var query = employees.Where(m => m.Name.Contains("A"));
if (roleId != 0)
{
    query = query?.Where(m => m.RoleId == roleId);
}
query = query.ToList();
foreach (var item in query)
{
    Console.WriteLine($"Id: {item.Id}, Name:{item.Name}, Address:{item.Address}, Role Id={item.RoleId}");
} 
Refer the above code. Herer i have passed RoleId as 2. Now as per condition if role id is not zero condition will get applied, and at the end it will return filter data. Now let's run the code to check output. Ones we run code break point will execute. Here we will see, we are not able to get the list. Please refer below output.

Linq data

Here after adding where clause we are not able to get user list. Now let's execute the next line and check detail.
Linq data after query execution

Here we can see we are able to get 2 records after applying .ToList(). Now let's press F5 to check output.

Linq data after execution

Post a Comment