C# + EntityFramework:通过查询将多个组转换为嵌套 JSON

Posted

技术标签:

【中文标题】C# + EntityFramework:通过查询将多个组转换为嵌套 JSON【英文标题】:C# + EntityFramework: Convert multiple group by query to nested JSON 【发布时间】:2015-09-12 14:51:40 【问题描述】:

我做了以下 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 的唯一项目数

【问题讨论】:

你能解释一下除了格式之外你想做什么吗? IE。子区域是区域字符串的子区域 刚刚用层次结构编辑了答案。希望这会有所帮助 你可以用我现在正在尝试解决的字典字典来做到这一点 【参考方案1】:

这是您想要的 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,因为我不太明白你在数什么。

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

【讨论】:

以上是关于C# + EntityFramework:通过查询将多个组转换为嵌套 JSON的主要内容,如果未能解决你的问题,请参考以下文章

EntityFramework 模型的通用 Getter

EF6学习笔记

C# EntityFramework Code First 迁移

c#中mvc添加类库写实体类怎么写

EntityFramework和EntityFramework.Extended使用说明——性能,语法和产生的sql

C# 数据操作系列 - 5. EF Core 入门