Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
335 views
in Technique[技术] by (71.8m points)

c# - Entity Framework List Contains in lambda

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
  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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

56.8k users

...