繁体   English   中英

如何在对列表进行分组后获得嵌套的 json 结果?

[英]How to get a nested json result after grouping a list?

我想通过按类别、商家和优惠券对列表进行分组,将以下列表转换为 JSON 结果(JSON 结果也如下所示)。 我尝试使用 LINQ 查询来解决此问题,但无法解决。

需要将以下列表转换为 JSON 结果。

    var list = new List<Data>() { 
    new Data { categoryCode = "1", categoryName = "A", merchantId = "1", mechantName = "Shehan", voucherId = "200", voucherDescription = "Voucher - 200"}, 
    new Data { categoryCode = "1", categoryName = "A", merchantId = "1", mechantName = "Shehan", voucherId = "300", voucherDescription = "Voucher - 300"}, 
    new Data { categoryCode = "1", categoryName = "A", merchantId = "2", mechantName = "Ashen", voucherId = "200", voucherDescription = "Voucher - 200"},
    new Data { categoryCode = "2", categoryName = "B", merchantId = "1", mechantName = "Shehan", voucherId = "200", voucherDescription = "Voucher - 200"},
    new Data { categoryCode = "2", categoryName = "B", merchantId = "1", mechantName = "Shehan", voucherId = "300", voucherDescription = "Voucher - 300"},
    new Data { categoryCode = "2", categoryName = "B", merchantId = "2", mechantName = "Ashen", voucherId = "200", voucherDescription = "Voucher - 200"},
    new Data { categoryCode = "2", categoryName = "B", merchantId = "2", mechantName = "Ashen", voucherId = "300", voucherDescription = "Voucher - 300"},
};

这是列表中使用的数据 Model。

class Data
{
    public string categoryCode { get; set; }
    public string categoryName { get; set; }
    public string merchantId { get; set; }
    public string mechantName { get; set; }
    public string voucherId { get; set; }
    public string voucherDescription { get; set; }
}

需要从给定列表中获取以下 JSON 结果。

"Data": [
   {
      "categoryCode":"1",
      "categoryName":"A",
      "merchants":[
         {
            "merchantId":"1",
            "merchantName":"Shehan",
            "vouchers":[
               {
                  "Id":"200",
                  "description":"Voucher - 200"
               },
               {
                  "Id":"300",
                  "description":"Voucher - 300"
               }
            ]
         },
         {
            "merchantId":"2",
            "merchantName":"Ashen",
            "vouchers":[
               {
                  "Id":"200",
                  "description":"Voucher - 200"
               }
            ]
         }
      ]
   },
   {
      "categoryCode":"2",
      "categoryName":"B",
      "merchants":[
         {
            "merchantId":"1",
            "merchantName":"Shehan",
            "vouchers":[
               {
                  "Id":"200",
                  "description":"Voucher - 200"
               },
               {
                  "Id":"300",
                  "description":"Voucher - 300"
               }
            ]
         },
         {
            "merchantId":"2",
            "merchantName":"Ashen",
            "vouchers":[
               {
                  "Id":"200",
                  "description":"Voucher - 200"
               },
               {
                  "Id":"300",
                  "description":"Voucher - 300"
               }
            ]
         }
      ]
   }
]

如果您能提供帮助,我将不胜感激。 :)

我认为你可以通过这种方式做到这一点:

 var result = list.GroupBy(x => new { x.categoryCode, x.categoryName })
                .Select(x => new
                {
                    x.Key.categoryCode,
                    x.Key.categoryName,
                    Merchants = x.ToList().GroupBy(g => new { g.merchantId, g.mechantName })
                        .Select(v => new
                        {
                            v.Key.merchantId,
                            v.Key.mechantName,
                            vouchers = v.Select(s => new { Id = s.voucherId, description = s.voucherDescription })
                                .ToList()
                        })
                });

            var json = JsonConvert.SerializeObject(result);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM