为什么ToLookup和GROUPBY有什么不同? [英] Why are ToLookup and GroupBy different?

查看:905
本文介绍了为什么ToLookup和GROUPBY有什么不同?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

.ToLookup< TSource,TKEY的> 返回 ILookup< TKEY的,TSource> ILookup< TKEY的,TSource> 也实现了接口的IEnumerable< IGrouping< TKEY的,TSource>>



.GroupBy< TSource,TKEY的> 返回的IEnumerable< IGroupking< TKEY,TSource>>



ILookup有方便的索引属性,所以它可以在一个类似字典(或查找类似)的方式使用,而GROUPBY能'T。没有索引GROUPBY是要工作的疼痛;相当多,那么你可以参考返回对象的唯一途径是通过其循环(或者使用其他LINQ的扩展方法)。换句话说,即GROUPBY作品任何情况下,ToLookup将正常工作。



这一切给我留下了一个问题:为什么我会永远跟GROUPBY费心呢?它为什么要存在?


解决方案

为什么我会永远跟GROUPBY费心呢?它为什么要存在?




当你调用ToLookup表示它与一个十亿行的远程数据库表的对象上会发生什么?



数十亿行通过网络发送,和你建立本地查找表。



会发生什么,当你?这样的对象调用GROUPBY



一个查询对象建立;故事的结束



当该查询对象枚举则表中的分析完成的在数据库服务器上和分组结果发送背部的需求的一次几个。



在逻辑上他们是一样的,但性能影响的每是完全不同的。调用ToLookup手段的我想要整个事情缓存,现在由集团组织的。调用GROUPBY的意思是我建立一个对​​象来表示这个问题将这些东西是什么样子,如果我按组安排他们呢?'


.ToLookup<TSource, TKey> returns an ILookup<TKey, TSource>. ILookup<TKey, TSource> also implements interface IEnumerable<IGrouping<TKey, TSource>>.

.GroupBy<TSource, TKey> returns an IEnumerable<IGroupking<Tkey, TSource>>.

ILookup has the handy indexer property, so it can be used in a dictionary-like (or lookup-like) manner, whereas GroupBy can't. GroupBy without the indexer is a pain to work with; pretty much the only way you can then reference the return object is by looping through it (or using another LINQ-extension method). In other words, any case that GroupBy works, ToLookup will work as well.

All this leaves me with the question why would I ever bother with GroupBy? Why should it exist?

解决方案

why would I ever bother with GroupBy? Why should it exist?

What happens when you call ToLookup on an object representing a remote database table with a billion rows in it?

The billion rows are sent over the wire, and you build the lookup table locally.

What happens when you call GroupBy on such an object?

A query object is built; end of story.

When that query object is enumerated then the analysis of the table is done on the database server and the grouped results are sent back on demand a few at a time.

Logically they are the same thing but the performance implications of each are completely different. Calling ToLookup means I want a cache of the entire thing right now organized by group. Calling GroupBy means "I am building an object to represent the question 'what would these things look like if I organized them by group?'"

这篇关于为什么ToLookup和GROUPBY有什么不同?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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