嵌套组Linq查询与Groupby扩展方法 [英] Nested Groups Linq query vs Groupby extension method

查看:217
本文介绍了嵌套组Linq查询与Groupby扩展方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图使用GroupBy扩展方法在嵌套分组下面进行转换.

I was trying to convert below nested grouping using GroupBy extension method.

        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;

这将返回一个IEnumerable<IGrouping<KeySelector, IGrouping<KeySelector, Object>>;产生树状结构-

This returns an IEnumerable<IGrouping<KeySelector, IGrouping<KeySelector, Object>>; Which produces a tree like structure -

FirstGroup -> Key(Date) 
   Elements - SecondGroup -> Key(Event)
       Elements - (TeamHistory) ..

但是当我尝试使用GroupBy扩展方法编写类似的查询时,它的工作方式似乎有所不同.根据Groupby方法的签名返回 IEnumerable<IGrouping<TKey, TSource>>.意味着如果我尝试嵌套,它会返回IEnumerable<IEnumerable<IGrouping<KeySelector, Elements>>>,这当然会产生非期望的结果.

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 IEnumerable<IGrouping<TKey, TSource>>. Means if I would try to do nesting it returns IEnumerable<IEnumerable<IGrouping<KeySelector, Elements>>> which ,ofcourse, would produce non-desired results.

这是我尝试过的:

    var res1 = teamHistories.GroupBy(x => x.Date, (key, g1) =>
                                    g1.Select(x => new { key, x })
                                         .GroupBy(x => x.x.Event, g => g));

问题-

  1. 这是进行这种嵌套的正确方法吗?因为我得到的结果是不同的.它返回的是IEnumerable<IEnumerable<IGrouping<KeySelector, Elements>>>而不是IEnumerable<KeySelector, IGrouping<KeySelector, IGrouping<KeySelector, Object>>>.

与Linq查询中的Group by子句相比,GroupBy扩展方法是否有任何限制?

Are there any limitations with GroupBy extension methods compared to Group by clause in Linq query?

如果有人想复制结果,则为完整的摘要.

Here's the complete snippet if someone wants to reproduce the result.

推荐答案

我认为您的第一个查询(在您的删除)将使用方法语法:

I think your first query (in your snipped) would be this way using method syntax:

   var res1 = teamHistories.Where(th => th.TeamName.ToLower() == "xxx")
                           .GroupBy(th=>th.Date)
                           .SelectMany (grp1 => grp1.GroupBy (th => th.Event), (grp1, grp2) => new {grp1 = grp1, grp2 = grp2})
                           .GroupBy (temp0 => temp0.grp1.Key, temp0 => temp0.grp2);

关于第二个问题,使用方法语法没有限制.实际上,在编译代码时,必须将使用查询语法的第一个查询转换为.NET公共语言运行库(CLR)的方法调用,因此两者在语义上是相同的,只是查询语法是语法糖,有助于使查询更简单,更简单.更容易阅读.

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.

这篇关于嵌套组Linq查询与Groupby扩展方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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