3

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

Community
  • 1
  • 1
Jonas Schmid
  • 5,360
  • 6
  • 38
  • 60

1 Answers1

7

Yes, LINQ 2 SQL would use the IN clause in this scenario.

But not because it is "smart" and detects that you have a huge DB and a small id list.

The IN clause is how LINQ 2 SQL "knows" to express the List.Contains in SQL.


In fact, if you had a list of 1000 items you'll see an IN clause 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.

Cristian Lupascu
  • 39,078
  • 16
  • 100
  • 137