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

查看:33
本文介绍了何时使用 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;

然后 'first1 == first2' 评估为假,即使两个 U 包装相同的 T.我还没有测试过,但我认为可以通过链接 .ToList() 或 .ToList() 来评估为真.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.如果您计划存储结果,或多次使用结果,那么将其存储在集合中会很有帮助.

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 通常很好,但根据预期的用例,您可能需要考虑使用 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(),这实际上取决于您将如何使用结果.与每个相关的成本几乎相同(如果输入不是 ICollectionToList() 实际上的执行开销略低).通常,我更喜欢 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天全站免登陆