ilookup

ILookup<TKey, TVal> vs. IGrouping<TKey, TVal>

旧城冷巷雨未停 提交于 2019-12-17 07:11:08
问题 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,

Does .GroupBy() guarantee order in its groupings?

百般思念 提交于 2019-11-29 13:26:24
Say I have an (ordered) sequence of animals: Eagle Elephant Tarantula Terrapin Tiger and I group by first letter: Animals.GroupBy(animal => animal.First()) will the elements of the IGrouping s in the resulting sequence be in the same order as the input sequence? Yes, they will be: GroupBy (MSDN) . The IGrouping<TKey, TElement> objects are yielded in an order based on the order of the elements in source that produced the first key of each IGrouping<TKey, TElement>. Elements in a grouping are yielded in the order they appear in source. Quote from the MSDN page for GroupBy: The IGrouping<TKey,

Does .GroupBy() guarantee order in its groupings?

走远了吗. 提交于 2019-11-28 07:00:28
问题 Say I have an (ordered) sequence of animals: Eagle Elephant Tarantula Terrapin Tiger and I group by first letter: Animals.GroupBy(animal => animal.First()) will the elements of the IGrouping s in the resulting sequence be in the same order as the input sequence? 回答1: Yes, they will be: GroupBy (MSDN). The IGrouping<TKey, TElement> objects are yielded in an order based on the order of the elements in source that produced the first key of each IGrouping<TKey, TElement>. Elements in a grouping

ILookup<TKey, TVal> vs. IGrouping<TKey, TVal>

﹥>﹥吖頭↗ 提交于 2019-11-27 03:09:47
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 =>