返回LINQ结果时何时调用ToList的经验法则 [英] Rules of thumb for when to call ToList when returning LINQ results

查看:81
本文介绍了返回LINQ结果时何时调用ToList的经验法则的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找在IEnumerables上调用ToList/ToArray/MemoizeAll(Rx)的经验法则,而不是在返回IEnumerable时返回查询本身.

I'm looking for rules of thumb for calling ToList/ToArray/MemoizeAll(Rx) on IEnumerables, as opposed to returning the query itself when returning IEnumerable of something.

通常,我发现最好返回查询并让调用者决定是否需要列表,但是由于linq的懒惰特性,有时它可以返回并在后面咬你.

Often I find that it is better to just return the query and let the caller decide whether a list is needed or not, but sometimes it can come back and bite you in the rear due to the lazy nature of linq.

我想收集以下准则:

在以下情况下调用ToList

Call ToList if:

  • 您创建新对象(例如,在选择中)
  • 您的查询有副作用

否则,返回查询

推荐答案

首先,您永远不要在查询中产生副作用.那是最糟糕的做法.查询应该回答一个问题,而不是产生影响.

First off, you should NEVER have side effects in a query. That is a worst practice. Queries should answer a question, not produce an effect.

您的问题的答案是:当呼叫者期望查询时返回查询;当呼叫者需要一个列表时,返回一个列表.在设计方法时,请确定调用者更可能想要的东西,实现该需求,然后记录它.

The answer to your question is: return a query when the caller expects a query; return a list when the caller expects a list. When you design your method, decide what the caller is more likely to want, implement that, and then document it.

在考虑调用方是查询还是列表时,请考虑查询和列表之间的区别:

When considering whether the caller wants a query or a list, think about the differences between queries and lists:

  • 查询始终是最新的.如果查询所查询的对象/数据库/无论其内容如何,​​那么如果再次运行查询,查询结果将发生变化.列表不会更改其内容,因此列表已过时.如果您的呼叫者需要最新数据,请给他们查询.如果他们需要可以随意检查的数据快照,请给他们一个列表.

  • queries are always up-to-date. If the objects/databases/whatever that the query queries against changes its content, then the query results will change if you run the query again. Lists don't change their contents and therefore lists get out of date. If your caller requires the latest data then give them a query. If they require a snapshot of the data that they can inspect at leisure then give them a list.

查询以获取其结果可能非常昂贵.列表很便宜,无法获得其结果.如果呼叫者可能想多次询问该结果,并希望每次都能得到相同的结果,请给他们一个列表.

queries are potentially expensive to execute to obtain their results. Lists are cheap to obtain their results. If the caller is likely to want to interrogate the result many times and expects to get the same results each time then give them a list.

构造查询很快.执行查询以构建列表的过程是 slow .列表始终获得查询的所有结果.调用者可能希望通过例如仅采用前十个元素来进一步限制查询.如果调用者不希望或不需要承担对整个查询进行完全迭代的费用,则给他们查询;不要代表他们做这个决定,而要给他们一个清单.

Constructing a query is fast. Executing a query to construct a list is slow. A list always obtains all the results of a query. The caller might want to further restrict the query, by, say, taking only the first ten elements. If the caller does not want or need to take on the expense of fully iterating over the entire query then give them a query; don't make that decision on their behalf and give them a list.

查询是 tiny .列表是 big .可以在O(1)空间中的n个项目上迭代许多查询;具有n个项目的列表占用O(n)空间.如果结果集很大,则将其放入列表中可能效率不高.

queries are tiny. Lists are big. Many queries can be iterated over n items in O(1) space; a list with n items takes up O(n) space. If the result set is enormous then putting it in a list is probably inefficient.

,依此类推.

没有简单的答案.答案与其他任何设计问题的答案相同:在功能用户最希望的范围内考虑每种可能解决方案的利弊,然后选择一个合理的折衷解决方案

There is no easy answer. The answer is the same as the answer to any other design problem: Consider all the pros and cons of each possible solution in the context of what is most likely wanted by the user of the feature, and then pick a reasonable compromise solution.

这篇关于返回LINQ结果时何时调用ToList的经验法则的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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