Entity Framework List Contains in lambda

.net c# entity-framework entity-framework-6 linq

Question

I want to query items with specific IDs using. For example:

var ids = new List<int> { 1, 3, 5 };

var items = context.Items.Where(item => ids.Contains(item.ID)).ToList();

Questions:

  1. Will this generate a single query with SQL IN operator?
  2. Is this code OK in terms of performance?
  3. Are there any better ways to do it?

I am using Entity Framework 6 with Microsoft SQL Server.

1
6
4/26/2016 11:52:06 AM

Accepted Answer

  1. Will this generate a single query with SQL IN operator?
    Yes
  2. Is this code OK in terms of performance?
    Yes (for small lists)
  3. Are there any better ways to do it?
    No (for small lists)

If the list is really big and the table is reasonably small you might get better performance bringing the complete table into memory and do an in memory join with the list.

6
9/23/2015 5:11:08 PM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow