C# + Entity Framework : Convert multiple group by query to nested JSON

标签 c# asp.net json linq entity-framework

我做了下面的linq语句

C#

var list = from row in repository.GetAllEntities()
                       group row by new { row.RegionString, row.SubRegionString, row.CountryString } into g
                       select new { g.Key.RegionString, g.Key.SubRegionString, g.Key.CountryString, Count = g.Count() };

return Json(list, JsonRequestBehavior.AllowGet);

返回

[
  {
    "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 是 SubRegion 的父级,SubRegion 是 Country 的父级,Count 是属于 Country 的项目的唯一数量

最佳答案

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

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 };  

此查询中没有Count,因为我不太明白您在计算什么。

我在这里所做的是将行分组到区域中,在最后一行中您可以看到
选择新的 { Name = regions.Key, ... } 我要返回区域的部分。
要获取子项,您需要将区域分组为子区域(与区域相同)。你一直重复这个直到国家,你就完成了。

关于C# + Entity Framework : Convert multiple group by query to nested JSON,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31059822/

相关文章:

c# - 使用 C# 获取特定 XML 元素值的问题

c# - 使用 linq 将 xml 文件加载到二维矩形数组中

c# - Asp.net mvc 不会接受 razor 参数

java - Spring boot 将 JSON 反序列化为多态类型

c# - 如何使 autofac KeyFilter 在 asp.net-core web api 中工作?

c# - EF6 - 空间/全文/哈希索引和显式索引顺序的使用不正确

c# - 在 C# 中查找两个大小相等的数组的所有组合集

c# - 在 C# 中将日期格式从 mm-dd-yyyy 转换为 dd-mmm-yyyy

json - 如何使用数据工厂将 JSON 数据从 REST API 映射到 Azure SQL

java - Android JSon 反序列化中无法识别的字段