Dynamic where condition in LINQ
I have a scenario where I have to use a dynamic where condition in LINQ. I want something like this: public void test(bool flag) { from e in employee where e.Field<string>("EmployeeName") == "Jhom" If (flag == true) { e.Field<string>("EmployeeDepartment") == "IT" } select e.Field<string>("EmployeeID") } I know we can't use the 'If' in the middle of the Linq query but what is the solution for this? Please help... So, if flag is false you need all Jhoms, and if flag is true you need only the Jhoms in the IT department This condition !flag || (e.Field<string>("EmployeeDepartment") == "IT"