何时使用LINQ的.ToList()或.ToArray() [英] When to use LINQ's .ToList() or .ToArray()

查看:74
本文介绍了何时使用LINQ的.ToList()或.ToArray()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

运行此代码后:

var input = new List<T>( ... );
var result = input.Select( t => new U(t) );

U first1 = null;
foreach ( U u1 in result )
    if ( first1 == null )
        first1 = u1;

U first2 = null;
foreach ( U u2 in result )
    if ( first2 == null )
        first2 = u2;

然后,即使两个U都包装相同的T,"first1 == first2"的计算结果仍为false.我尚未对其进行测试,但我认为可以通过链接.ToList()或将其计算为true.将ToArray()调用到Select()上.

Then 'first1 == first2' evaluates to false even though both U's wrap the same T. I haven't tested it yet, but I think it can be made to evaluate to true by chaining a .ToList() or .ToArray() onto the Select() call.

在实际代码中(比此简单说明复杂得多),用于确定应附加.ToList()还是.ToArray()的良好经验法则是什么?我最初的想法是,可能会多次重复的任何引用表达式,或者,为了更安全地防止潜在的重复出现,它的结果永远不会改变.

In real code, which is much more complex than this simple illustration, what is a good rule of thumb to use for deciding if .ToList() or .ToArray() should be appended? My initial thoughts are either any referenced expression that may be iterated more than once or, to be safer in case potential iterations are not obvious, any referenced expression whose result will never change.

推荐答案

不幸的是,我认为这里没有良好的硬性规定".这很大程度上取决于您期望如何使用结果以及查询本身的实际作用.

Unfortunately, I don't think there is a good "hard and fast" rule here. It depends a lot on how you expect the results to be used, and what the query itself is actually doing.

我最初的想法是可能重复多次的任何表达式,或者为安全起见,以防潜在的迭代不明显,其结果永远不会改变.

My initial thoughts are either any expression that may be iterated more than once or, to be safer in case potential iterations are not obvious, any expression whose result will never change.

通常,如果要多次使用查询结果,最好通过ToList()ToArray()存储它.如果您的LINQ查询是昂贵的"查询,则尤其如此,因为它可以防止昂贵的操作多次运行.

In general, if you're going to use the result of a query more than once, it's always a good idea to store it via ToList() or ToArray(). This is especially true if you're LINQ query is an "expensive" one, as it prevents the expensive operation from running more than once.

通常,如果只想枚举结果,那么我将其保留为IEnumerable<T>.如果您打算存储结果,或者不止一次使用结果,那么将其存储在集合中将是有益的.

Typically, if you're only going to enumerate the results, then I would leave it as IEnumerable<T>. If you plan to store the results, or use the results more than once, then storing it in a collection can be beneficial.

要注意的另一个地方是,如果您在公共API中返回结果.返回IEnumerable<T>通常很不错,但根据预期的使用情况,您可能需要考虑使用ToList()来防止该操作被多次执行.

The other place to watch for is if you return the results in a public API. While it's often nice to return IEnumerable<T>, depending on the expected use case, you may want to consider using ToList() to prevent the operation from being executed more than once.

关于使用ToList()还是ToArray(),这实际上取决于您如何使用结果.与每一个相关的成本几乎是相同的(如果输入不是ICollection<T>,则ToList()实际上具有较低的执行开销).通常,除非我对数组有特殊需要,否则我更喜欢ToList()而不是ToArray().

As for whether to use ToList() or ToArray(), it really depends on how you'll use the results. The cost associated with each is nearly identical (ToList() actually has slightly lower execution overhead if the input is not ICollection<T>). Typically, I prefer ToList() over ToArray() unless I have a specific need or desire for an array.

这篇关于何时使用LINQ的.ToList()或.ToArray()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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