0

希望在 linq 中对 Nested group by 有所帮助,我正在尝试降低三个级别。

基本上这条路会从BusinessBusUnitLevel1

因此,在 Business 下有各种 BusUnits,在 BusUnits 下有各种 level1 值

我想将此作为 Json 对象返回。

正如我现在所拥有的那样,我来到了 BusUnits

如下

var queryNestedData = (from r in DataItems
                      group r by r.Business into businesses
                      from businessUnits in
                       (from r in businesses
                        group r by r.Businessunit)
                        group businessUnits by businesses.Key).Select(tg =>
                        new
                        {
                           Business = tg.Key,
                           BusinessUnits = tg.Select(tv => new { BusinessUnit = tv.Key })
                   });

我怎样才能到达 Level1 以便也返回值?

4

1 回答 1

1

您可以指定要包含在查询结果中的相关对象。确切的代码片段取决于您使用的 LINQ 提供程序。对于实体框架,它将是:

... from r in DataItems.Include(di => di.Business.Businessunit.Level1) ...
于 2013-10-03T10:38:33.967 回答