[英]How to get a nested json result after grouping a list?
I want to convert following list to JSON result by grouping the list by Category, Merchants and Vouchers (JSON result also shown below).我想通过按类别、商家和优惠券对列表进行分组,将以下列表转换为 JSON 结果(JSON 结果也如下所示)。 I tried to solve this issue using LINQ queries, but could not do it.
我尝试使用 LINQ 查询来解决此问题,但无法解决。
Need to convert following List to JSON result.需要将以下列表转换为 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"},
};
This is the Data Model used in the list.这是列表中使用的数据 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; }
}
Need to get the following JSON Result from the given list.需要从给定列表中获取以下 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"
}
]
}
]
}
]
Really appreciate if you could help on this.如果您能提供帮助,我将不胜感激。 :)
:)
I think you can do that by this way:我认为你可以通过这种方式做到这一点:
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.