简体   繁体   English

EF-Linq表达式并使用整数列表来获得最佳性能

[英]EF - Linq Expression and using a List of Ints to get best performance

So I have a list(table) of about 100k items and I want to retrieve all values that match a given list. 所以我有一个约100k项的列表(表),我想检索与给定列表匹配的所有值。

I have something like this. 我有这样的东西。 the Table Sections key is NOT a primary key, so I'm expecting each value in listOfKeys to return a few rows. Table Sections键不是主键,因此我期望listOfKeys中的每个值都返回几行。

List<int> listOfKeys = new List<int>(){1,3,44};
var allSections = Sections.Where(s => listOfKeys.Contains(s.id));

I don't know if it makes a difference but generally listOfKeys will only have between 1 to 3 items. 我不知道这是否有所不同,但通常listOfKeys仅包含1到3个项目。

I'm using the Entity Framework. 我正在使用实体框架。

So my question is, is this the best / fastest way to include a list in a linq expression? 所以我的问题是,这是在linq表达式中包含列表的最佳/最快方法吗? I'm assuming that it isn't better to use another .NET ICollection data object. 我假设最好不要使用另一个.NET ICollection数据对象。 Should I be using a Union or something? 我应该使用工会之类的东西吗?

Thanks 谢谢

Suppose the listOfKeys will contain only small about of items and it's local list (not from database), like <50, then it's OK. 假设listOfKeys仅包含约一小项,并且是本地列表(不是来自数据库),例如<50,那么就可以了。 The query generated will be basically WHERE id in (...) or WHERE id = ... OR id = ... ... and that's OK for database engine to handle it. 生成的查询基本上WHERE id in (...) WHERE id = ... OR id = ... ...WHERE id = ... OR id = ... ... ,数据库引擎可以处理它。

A Join would probably be more efficient: Join可能会更有效:

var allSections =
    from s in Sections
    join k in listOfKeys on s.id equals k
    select s;

Or, if you prefer the extension method syntax: 或者,如果您更喜欢扩展方法语法:

var allSections = Sections.Join(listOfKeys, s => s.id, k => k, (s, k) => s);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM