I have to select some items from a database. The IDs of the items are in a List.
In an answer here, Jon Skeet states that we might use Contains()
var foo = channel.AsQueryable<CodeData>()
.Where(codeData => codeIDs.Contains(codeData.CodeId));
In a context where the database is huge and the IDs list is really small, how does it perform?
Is Linq smart enough to use the IN clause in SQL?
Edit: I am using DataContext and SQL Server 2008
Yes, LINQ 2 SQL would use the
INclause in this scenario.But not because it is “smart” and detects that you have a huge DB and a small id list.
The
INclause is how LINQ 2 SQL “knows” to express theList.Containsin SQL.In fact, if you had a list of 1000 items you’ll see an
INclause with 1000 parameters.If you have a list of more than 2100 ids, you’ll hit a limitation in the SQL Client that does not allow more than 2100 parameters on a command. But LINQ 2 SQL would happily generate a command that has, for example, even 5000 parameters.