Entity Framework List Contains in lambda

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

Question

I want to use to query objects with particular IDs. For instance:

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

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

Questions:

  1. Will this produce a single SQL query?IN operator?
  2. Is the performance of this code satisfactory?
  3. Is there a better way to go about it?

I'm using Microsoft SQL Server and Entity Framework 6 together.

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

Accepted Answer

  1. Will this produce a single SQL IN operator query?
    Yes
  2. Is the performance of this code satisfactory?
    Yes (for small lists)
  3. Is there a better way to go about it?
    No (for small lists)

Bringing the entire table into memory and doing an in memory join with the list might improve efficiency if the list is really large and the table is quite small.

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