LINQ TO SQL : WHERE X IN ()

June 22, 2009 – 7:44 pm

I recently needed to do a WHERE X IN () style query using LINQ to SQL. Here’s the solution I used:

List<int> ids = new List<int>();
foreach(var t in some_ids_collection) { ids.Add(t.ID); }

// Do the query...
var results = from x in db.SomeTable
where ids.Contains(x.SomeColumn)
select x;

Basically, you put the values you want between the () of your query into a List<> collection. Then you check a particular column of each row of the table you are querying from to see if its value is “contained” within the List<> collection.

Your Ad Here

Post a Comment