LINQ To Entity

Linq to Entity is in the market for a while..

Recently I faced a problem on how to use SQL IN Clause in LINQ.

Suppose you want to execute a query like:

SELECT * FROM TABLE1 WHERE COLUMN1 in (Value1, Value2)

in order to form the In clause in LINQ, one need to create a List.

Here is the code to create a list:

Suppose you want to search users of two departments eg 1,2


List<int> dept = new List<int>();
dept.Add(1);
dept.Add(2);



The search query will be like this:


var query = from lst in dc.UserMasters
where dept.Contains(lst.DeptId)
select lst;


where lst is the result container,
UserMasters is the entity
dc is the datacontext


Comments

No responses found. Be the first to comment...


  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name:
    Email: