问题
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:
- Will this generate a single query with SQL
INoperator? - Is this code OK in terms of performance?
- Are there any better ways to do it?
I am using Entity Framework 6 with Microsoft SQL Server.
回答1:
- Will this generate a single query with SQL IN operator?
Yes - Is this code OK in terms of performance?
Yes (for small lists) - 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.
来源:https://stackoverflow.com/questions/32745399/entity-framework-list-contains-in-lambda