C#和#x2B;EntityFramework:将多个分组按查询转换为嵌套JSON

C#和#x2B;EntityFramework:将多个分组按查询转换为嵌套JSON,c#,asp.net,json,linq,entity-framework,C#,Asp.net,Json,Linq,Entity Framework,我发表了以下linq声明 C# 返回 [ { "RegionString":"Americas", "SubRegionString":"", "CountryString":"", "Count":2 }, { "RegionString":"Americas", "SubRegionString":"NorthAmerica", "CountryString":"Canada", "Count":5 }, {

我发表了以下linq声明

C#

返回

[
  {
    "RegionString":"Americas",
    "SubRegionString":"",
    "CountryString":"",
    "Count":2
  },
  {
    "RegionString":"Americas",
    "SubRegionString":"NorthAmerica",
    "CountryString":"Canada",
    "Count":5
  },
  {
    "RegionString":"Americas",
    "SubRegionString":"NorthAmerica",
    "CountryString":"US",
    "Count":3
  },
  {
    "RegionString":"Americas",
    "SubRegionString":"SouthAmerica",
    "CountryString":"Chile",
    "Count":3
  },
  {
    "RegionString":"EMEA",
    "SubRegionString":"AsiaPacific",
    "CountryString":"Australia",
    "Count":2
  },
  {
    "RegionString":"EMEA",
    "SubRegionString":"AsiaPacific",
    "CountryString":"Japan",
    "Count":1
  },
  {
    "RegionString":"EMEA",
    "SubRegionString":"SouthernEurope",
    "CountryString":"Turkey",
    "Count":1
  },
  {
    "RegionString":"EMEA",
    "SubRegionString":"WesternEurope",
    "CountryString":"",
    "Count":1
  }
]
但我正试图把它变成这种形式

[{
    name: "Americas",
    children: [
     {
         name: "NorthAmerica",
         children: [{ "name": "Canada", "count": 5 },
                    { "name": "US", "count": 3 }]

     },
     {
         name: "SouthAmerica",
         children: [{ "name": "Chile", "count": 1 }]
     },
    ],
},
{
    name: "EMA",
    children: [
     {
         name: "AsiaPacific",
         children: [{ "name": "Australia", "count": 2 },
                    { "name": "Japan", "count": 1 }]

     },
     {
         name: "SouthernEurope",
         children: [{ "name": "Turkey", "count": 1 }]
     },
    ],
}]
我如何修改语句以获得我想要的结果?非linq答案也可以接受


编辑:Region是Subsection的父级,Subsection是Country的父级,Count是属于Country的项目的唯一数量

这是您想要的linq查询(为了可读性,我删除了
-String
后缀):

在这个查询中没有
Count
,因为我真的不明白你在数什么

我在这里做的是将行分组到区域中,在最后一行中,您可以看到
选择新的{Name=regions.Key,…}
返回区域的部分。

要获取子对象,需要将区域分组为子区域(与区域相同)。您在各个国家/地区重复这一过程,就完成了。

您能解释一下除了格式之外您还想做什么吗?即,sub region是刚刚用层次结构编辑答案的region Strings的子级。希望这是有帮助的,你也许可以用一本字典来做这件事,我现在正试图找到一个解决办法
[{
    name: "Americas",
    children: [
     {
         name: "NorthAmerica",
         children: [{ "name": "Canada", "count": 5 },
                    { "name": "US", "count": 3 }]

     },
     {
         name: "SouthAmerica",
         children: [{ "name": "Chile", "count": 1 }]
     },
    ],
},
{
    name: "EMA",
    children: [
     {
         name: "AsiaPacific",
         children: [{ "name": "Australia", "count": 2 },
                    { "name": "Japan", "count": 1 }]

     },
     {
         name: "SouthernEurope",
         children: [{ "name": "Turkey", "count": 1 }]
     },
    ],
}]
var list =
    from entity in repository.GetAllEntities()
    group entity by entity.Region into regions
    let childrenOfRegions =
        from region in regions
        group region by region.SubRegion into subregions
        let countriesOfSubRegions =
            from subregion in subregions
            group subregion by subregion.Country into countries
            select new { Name = countries.Key }
        select new { Name = subregions.Key, Children = countriesOfSubRegions }
    select new { Name = regions.Key, Children = childrenOfRegions };