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