的最快方法是什么

的最快方法是什么

本文介绍了Linq - 找出延迟执行的最快方法是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

找出哪些 .net 框架 linq 方法(例如 .IEnumerable linq 方法)使用延迟执行与未使用延迟执行实现的最快方法是什么.

What is the quickest way to find out which .net framework linq methods (e.g .IEnumerable linq methods) are implemented using deferred execution vs. which are not implemented using deferred execution.

虽然编码很多次,但我想知道这是否会以正确的方式执行.找出答案的唯一方法是访问 MSDN 文档以进行确认.是否有任何更快的方法,任何目录,网络上某处的任何列表,任何备忘单,任何其他可以分享的技巧?如果是,请这样做.这将帮助许多 linq 菜鸟(如我)减少错误.唯一的另一种选择是检查文档,直到人们使用它们足以记住(这对我来说很难,我倾向于不记得在某处记录并且可以查找的任何东西":D).

While coding many times, I wonder if this one will be executed right way. The only way to find out is go to MSDN documentation to make sure. Would there be any quicker way, any directory, any list somewhere on the web, any cheat sheet, any other trick up your sleeve that you can share? If yes, please do so. This will help many linq noobs (like me) to make fewer mistakes. The only other option is to check documentation until one have used them enough to remember (which is hard for me, I tend not to remember "anything" which is documented somewhere and can be looked up :D).

推荐答案

通常返回序列的方法使用延迟执行:

Generally methods that return a sequence use deferred execution:

IEnumerable<X> ---> Select ---> IEnumerable<Y>

返回单个对象的方法不会:

and methods that return a single object doesn't:

IEnumerable<X> ---> First ---> Y

所以,像WhereSelectTakeSkipGroupBy 和 OrderBy 使用延迟执行,因为它们可以,而像 FirstSingleToList 这样的方法ToArray 不要因为他们不能.

So, methods like Where, Select, Take, Skip, GroupBy and OrderBy use deferred execution because they can, while methods like First, Single, ToList and ToArray don't because they can't.

还有两种类型的延迟执行.例如,Select 方法在被要求生成一个项目时一次只会得到一个项目,而 OrderBy 方法在被要求返回时将不得不消耗整个源第一项.因此,如果您在 Select 之后链接 OrderBy,执行将被推迟,直到您获得第一个项目,但随后 OrderBy 会询问所有项目的 Select.

There are also two types of deferred execution. For example the Select method will only get one item at a time when it's asked to produce an item, while the OrderBy method will have to consume the entire source when asked to return the first item. So, if you chain an OrderBy after a Select, the execution will be deferred until you get the first item, but then the OrderBy will ask the Select for all the items.

这篇关于Linq - 找出延迟执行的最快方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-15 03:45