I was trying to convert below nested grouping using 'Sessions, Pages/session, ... '
extension method.
GroupBy
This returns an var res = from th in teamHistories
group th by th.Date into grp1
from grp2 in
(from th in grp1 group th by th.Event)
group grp2 by grp1.Key;
; Which produces a tree like structure -
IEnumerable<IGrouping<KeySelector, IGrouping<KeySelector, Object>>
But when I tried to write similar query using GroupBy extension method it seems to work differently. According to signature of Groupby method it returns
FirstGroup -> Key(Date)
Elements - SecondGroup -> Key(Event)
Elements - (TeamHistory) ..
. Means if I would try to do nesting it returns IEnumerable<IGrouping<TKey, TSource>>
which ,ofcourse, would produce non-desired results.
Here's what I tried:
IEnumerable<IEnumerable<IGrouping<KeySelector, Elements>>>
Question -
Is this correct way of doing such nesting? Because the results I'm getting are different. It's returning var res1 = teamHistories.GroupBy(x => x.Date, (key, g1) =>
g1.Select(x => new { key, x })
.GroupBy(x => x.x.Event, g => g));
instead of IEnumerable<IEnumerable<IGrouping<KeySelector, Elements>>>
.
Are there any limitations with IEnumerable<KeySelector, IGrouping<KeySelector, IGrouping<KeySelector, Object>>>
extension methods compared to GroupBy
clause in Linq query?
Here's the complete snippet if someone wants to reproduce the result.
答案 0 :(得分:3)
I think your first query (in your snipped) would be this way using method syntax:
&
About your second question there is no limitation using method syntax. In fact your first query using query syntax must be translated into method calls for the .NET common language runtime (CLR) when the code is compiled, so both are semantically identical, just that query syntax is syntactic sugar that helps to make queries simpler and easier to read.