IN queries with LINQ to SQL
This is another awesome feature in LINQ to SQL that i always forget about. Sometimes you have an array of strings or any other type and want to query the database for all values that have one of the values in the array.
string platform = "windows|linux";
string[] platformList = platform.Split('|');
itemQuery = from m in itemQuery
from p in m.PlattformItemRelations
where platformList.Contains(p.Plattform.Text.ToLower())
select m;
Ain’t that a beauty? I got this tip from Ron Connery so all the kudos to him.
Super cool, but be careful with the use.
On SQL 2005/2008 you can have at maximum 2100 parameters per stored procedure (http://msdn.microsoft.com/en-us/library/ms143432.aspx) and the way LINQ to SQL generate the query and call your database, if you have more than 2100 elements (minus the number of other parameters) in the array, an exception will be thrown.
Ahh! Should keep that in mind even thou i probably would not use that for a scenario with a large array. Seems like that should be solved in some other way.