C# 我应该在 LINQ 查询中使用两个“where”子句还是“&&”?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/664683/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Should I use two "where" clauses or "&&" in my LINQ query?
提问by Dаn
When writing a LINQ query with multiple "and" conditions, should I write a single where
clause containing &&
or multiple where
clauses, one for each conditon?
在编写具有多个“和”条件的 LINQ 查询时,我应该编写一个where
包含&&
或多个where
子句的子句,每个条件一个?
static void Main(string[] args)
{
var ints = new List<int>(Enumerable.Range(-10, 20));
var positiveEvensA = from i in ints
where (i > 0) && ((i % 2) == 0)
select i;
var positiveEvensB = from i in ints
where i > 0
where (i % 2) == 0
select i;
System.Diagnostics.Debug.Assert(positiveEvensA.Count() ==
positiveEvensB.Count());
}
Is there any difference other than personal preference or coding style (long lines, readability, etc.) between positiveEvensAand positiveEvensB?
除了个人偏好或编码风格(长行、可读性等)之外,positiveEvensA和positiveEvensB之间还有什么区别吗?
One possible difference that comes to mind is that different LINQ providers may be able to better cope with multiple where
s rather than a more complex expression; is this true?
想到的一个可能的区别是,不同的 LINQ 提供程序可能能够更好地处理多个where
s 而不是更复杂的表达式;这是真的?
采纳答案by Reed Copsey
I personally would always go with the && vs. two where clauses whenever it doesn't make the statement unintelligible.
我个人总是会使用 && 与两个 where 子句,只要它不会使语句变得难以理解。
In your case, it probably won't be noticeble at all, but having 2 where clauses definitely will have a performance impact if you have a large collection, and if you use all of the results from this query. For example, if you call .Count() on the results, or iterate through the entire list, the first where clause will run, creating a new IEnumerable that will be completely enumerated again, with a second delegate.
在您的情况下,它可能根本不会引起注意,但是如果您有一个大型集合,并且如果您使用此查询的所有结果,那么拥有 2 个 where 子句肯定会对性能产生影响。例如,如果您对结果调用 .Count() 或遍历整个列表,第一个 where 子句将运行,创建一个新的 IEnumerable,它将再次完全枚举,并带有第二个委托。
Chaining the 2 clauses together causes the query to form a single delegate that gets run as the collection is enumerated. This results in one enumeration through the collection and one call to the delegate each time a result is returned.
将 2 个子句链接在一起会导致查询形成一个委托,该委托在枚举集合时运行。这导致在每次返回结果时对集合进行一次枚举并调用一次委托。
If you split them, things change. As your first where clause enumerates through the original collection, the second where clause enumerates it's results. This causes, potentially (worst case), 2 full enumerations through your collection and 2 delegates called per member, which could mean this statement (theoretically) could take 2x the runtime speed.
如果你把它们分开,事情就会改变。当您的第一个 where 子句枚举原始集合时,第二个 where 子句枚举它的结果。这可能会导致(最坏的情况)通过您的集合进行 2 个完整枚举,每个成员调用 2 个委托,这可能意味着此语句(理论上)可能需要 2 倍的运行速度。
If you do decide to use 2 where clauses, placing the more restrictive clause first will help quite a bit, since the second where clause is only run on the elements that pass the first one.
如果您确实决定使用 2 个 where 子句,那么将限制性更强的子句放在首位会有很大帮助,因为第二个 where 子句仅在通过第一个 where 子句的元素上运行。
Now, in your case, this won't matter. On a large collection, it could. As a general rule of thumb, I go for:
现在,就您而言,这无关紧要。在大型集合中,它可以。作为一般经验法则,我选择:
1) Readability and maintainability
1)可读性和可维护性
2) Performance
2) 性能
In this case, I think both options are equally maintainable, so I'd go for the more performant option.
在这种情况下,我认为这两个选项都具有同等的可维护性,因此我会选择性能更高的选项。
回答by JaredPar
This is mostly a personal style issue. Personally, as long as the where
clause fits on one line, I group the clauses.
这主要是个人风格问题。就个人而言,只要该where
子句适合一行,我就会将这些子句分组。
Using multiple where
s will tend to be less performant because it requires an extra delegate invocation for every element that makes it that far. However it's likely to be an insignificant issue and should only be considered if a profiler shows it to be a problem.
使用多个where
s 的性能往往会降低,因为它需要对每个元素进行额外的委托调用,使其达到那么远。然而,这可能是一个微不足道的问题,只有在分析器显示它是一个问题时才应该考虑。
回答by DotnetDude
Like others have suggested, it's more of a personal preference. I like the use of && as it's more readable and mimics the syntax of other mainstream languages.
就像其他人所建议的那样,这更多是个人喜好。我喜欢 && 的使用,因为它更具可读性并且模仿了其他主流语言的语法。
回答by John Wardale
The performance issue only applies to memory based collections ... Linq to SQL generates expression trees that defer execution. More Details here:
性能问题仅适用于基于内存的集合...... Linq to SQL 生成延迟执行的表达式树。更多细节在这里: