本文介绍了ILookup< TKEY的,TVal>与IGrouping< TKEY的,TVal>的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直有麻烦阐述和,并很好奇,如果我理解正确了。 LINQ通过产生的 IGrouping 项目序列同时也给了我一个 ToLookup 扩展方法加剧了问题。所以,感觉就像他们一样,直到我看了更加紧密。

  VAR Q1 =
    从N N
    n组由n.MyKey为G
    选择克;
// Q1是IEnumerable的< IGrouping< TKEY的,TVal>>

这是等同于:

 无功Q2 = N.GroupBy(N => n.MyKey,N => N);
// Q2是IEnumerable的< IGrouping< TKEY的,TVal>>

这看起来很像:

  VAR Q3 = N.ToLookup(N => n.MyKey,N => N);
// Q3是ILookup< TKEY的,TVal>

我在下面的类比是否正确?


  1. IGrouping< TKEY的,TVal> 是一个单组(即键控顺序),类似于 KeyValuePair< TKEY的,TVal> 其中值实际上是元素的序列(而不是一个单一的元素)

  2. 的IEnumerable< IGrouping< TKEY的,TVal>> 就是其中的一个序列(类似于你会得到什么了一个的IDictionary&LT迭代时; TKEY的,TVal>

  3. ILookup< TKEY的,TVal> 更像是一个的IDictionary< TKEY的,TVal> 其中值实际上元素序列


解决方案

是的,所有这些都是正确的。

ILookup< TKEY的,TValue> 还延伸的IEnumerable< IGrouping< TKEY的,TValue>> ,所以你可以在所有的键/收集对迭代以及(或代替)只看了特别的关键字。

我基本上认为 ILookup&LT的; TKEY的,TValue> 作为是像的IDictionary< TKEY的,IEnumerable的< TValue>>

请记住 ToLookup 是现在就做操作(立即执行),而一个 GROUPBY 是推迟。碰巧的是,与拉LINQ的作品,当你开始拉的方式 IGrouping 从a的结果S GROUPBY ,它有反正读取所有数据(因为你无法通过切换中途组),而在其他实现它可能是能够产生流的结果。 (它在推LINQ。我期望的LINQ to事件是相同的)

I've been having trouble articulating the differences between ILookup<TKey, TVal> and IGrouping<TKey, TVal>, and am curious if I understand it correctly now. LINQ compounded the issue by producing sequences of IGrouping items while also giving me a ToLookup extension method. So it felt like they were the same until I looked more closely.

var q1 =
    from n in N
    group n by n.MyKey into g
    select g;
// q1 is IEnumerable<IGrouping<TKey, TVal>>

Which is equivalent to:

var q2 = N.GroupBy(n => n.MyKey, n => n);
// q2 is IEnumerable<IGrouping<TKey, TVal>>

Which looks a lot like:

var q3 = N.ToLookup(n => n.MyKey, n => n);
// q3 is ILookup<TKey, TVal>

Am I correct in the following analogies?

  1. An IGrouping<TKey, TVal> is a single group (i.e. a keyed sequence), analogous to KeyValuePair<TKey, TVal> where the value is actually a sequence of elements (rather than a single element)
  2. An IEnumerable<IGrouping<TKey, TVal>> is a sequence of those (similar to what you get when iterating over an IDictionary<TKey, TVal>
  3. An ILookup<TKey, TVal> is more like a IDictionary<TKey, TVal> where the value is actually a sequence of elements
解决方案

Yes, all of those are correct.

And ILookup<TKey, TValue> also extends IEnumerable<IGrouping<TKey, TValue>> so you can iterate over all the key/collection pairs as well as (or instead of) just looking up particular keys.

I basically think of ILookup<TKey,TValue> as being like IDictionary<TKey, IEnumerable<TValue>>.

Bear in mind that ToLookup is a "do it now" operation (immediate execution) whereas a GroupBy is deferred. As it happens, with the way that "pull LINQ" works, when you start pulling IGroupings from the result of a GroupBy, it has to read all the data anyway (because you can't switch group midway through) whereas in other implementations it may be able to produce a streaming result. (It does in Push LINQ; I would expect LINQ to Events to be the same.)

这篇关于ILookup&LT; TKEY的,TVal&GT;与IGrouping&LT; TKEY的,TVal&GT;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

05-17 16:49