我应该用两个"其中"条款或QUOT;&安培;&安培;"在我的LINQ查询? [英] Should I use two "where" clauses or "&&" in my LINQ query?

查看:121
本文介绍了我应该用两个"其中"条款或QUOT;&安培;&安培;"在我的LINQ查询?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在写LINQ查询有多个和的条件,我应该写一个包含单个其中,条款&功放;&安培; 或多个其中,条款,每个conditon?

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?

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());
}

有没有之间的 positiveEvensA 的任何区别不是个人的preference或编码样式(长队,可读性等)等的 positiveEvensB 的?

Is there any difference other than personal preference or coding style (long lines, readability, etc.) between positiveEvensA and positiveEvensB?

这所想到的一个可能的不同之处在于不同的LINQ提供程序可能能够更好地应付多个其中,取值,而不是更复杂的EX pression;这是真的?

One possible difference that comes to mind is that different LINQ providers may be able to better cope with multiple wheres rather than a more complex expression; is this true?

推荐答案

我个人总是会去与和放大器;&安培;与2 where子句时,它不会使语句无法理解。

I personally would always go with the && vs. two where clauses whenever it doesn't make the statement unintelligible.

在你的情况下,它可能不会是都明显可言,但有2 WHERE子句肯定会影响性能,如果你有一个大的集合,如果你使用所有的结果,从这个查询。例如,如果调用.Count之间()的结果,或者遍历整个列表,第一where子句将运行,创建将再次被完全列举新的IEnumerable,与第二委托。

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条文一起导致查询,形成一个单一的委托,它获取运行的集合枚举。这导致一个枚举通过收集和调用一次委托结果返回各一次。

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.

如果你将它们分割,事情的变化。作为你的第一个where子句枚举通过原始集合,第二where子句中列举了它的结果。这会导致,可能(最坏的情况下),通过您的收藏2个整体枚举和2名代表每个成员调用,这可能意味着这个说法(理论上)可能需要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.

如果你决定使用2 where子句中,首先把更多的限制性条款将帮助了不少,因为第二where子句仅运行在通过第一个元素。

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.

现在,你的情况,这并不重要。在一个大的集合,它可以。根据经验,一般情况下,我去了:

Now, in your case, this won't matter. On a large collection, it could. As a general rule of thumb, I go for:

1)的可读性和可维护性

1) Readability and maintainability

2)性能

在这种情况下,我认为这两种选择都同样易于维护,所以我会去为更高性能的选择。

In this case, I think both options are equally maintainable, so I'd go for the more performant option.

这篇关于我应该用两个&QUOT;其中&QUOT;条款或QUOT;&安培;&安培;&QUOT;在我的LINQ查询?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆