Thursday, March 4, 2010

Using ‘IN’ clause in Linq

Everybody knows how to use ‘in’ clause in sql.This is a frequently used keyword but unfortunately not available in Linq as it is.But don’t worry we can simulate the same in Linq as follows.

List<Employee> emps=new List<Employee>()
{new Employee{Name="Joy"},new Employee {Name="George"}};
string []filters={"Joy","George"};

IEnumerable<Employee> result = from li
in emps
where filters.Contains(li.Name)
select li;

This is the reverse process of in clause.It is checking whether the filter array contains the given value.