productIdに基づいてリストアイテムを連結しようとしています。JSONの命名についてお詫びします。私はそれが最善ではないことを知っています、私はこれを制御できません。
リスト内のアイテムが同じproductIdを持っている場合、それらを連結して、変数プロパティ(costType、feeId、feeNameなど)を含むfees配列を使用して1つのアイテムを作成します(目的の結果を参照)。productIdが同じ場合、branchName、branchId、productName、およびbundleはすべて同じ値になります。
[{
"branchname": "Branch",
"branchid": "b7d79617-1c36-4be1-87b7-d7a910a5b72e",
"productid": "d029d731-92a6-4bd3-b020-1ad282af8308",
"productname": "Local Search",
"bundle": false,
"costtype": "Cost",
"feeid": "c3d5fb93-f8be-4be2-b5b8-dd1701614e05",
"feename": "Fee",
"discounttype": null,
"discountamount": null,
"baseprice": 20,
"grossprice": 20,
"vatrate": 20,
"vatamount": 4,
"netprice": 24
}, {
"branchname": "Branch",
"branchid": "b7d79617-1c36-4be1-87b7-d7a910a5b72e",
"productid": "d029d731-92a6-4bd3-b020-1ad282af8308",
"productname": "Local Search",
"bundle": false,
"costtype": "Sale",
"feeid": "7299a1fe-fe3d-43e2-b21a-3710e1bcd720",
"feename": "Standard Product Fee",
"discounttype": null,
"discountamount": null,
"baseprice": 60,
"grossprice": 60,
"vatrate": 20,
"vatamount": 12,
"netprice": 72
}
]
望ましい結果:
[{
"branchname": "Branch",
"branchid": "b7d79617-1c36-4be1-87b7-d7a910a5b72e",
"productid": "d029d731-92a6-4bd3-b020-1ad282af8308",
"productname": "Local Search",
"bundle": false,
"fees": [{
"costtype": "Cost",
"feeid": "c3d5fb93-f8be-4be2-b5b8-dd1701614e05",
"feename": "Fee",
"discounttype": null,
"discountamount": null,
"baseprice": 20,
"grossprice": 20,
"vatrate": 20,
"vatamount": 4,
"netprice": 24
}, {
"costtype": "Sale",
"feeid": "7299a1fe-fe3d-43e2-b21a-3710e1bcd720",
"feename": "Standard Product Fee",
"discounttype": null,
"discountamount": null,
"baseprice": 60,
"grossprice": 60,
"vatrate": 20,
"vatamount": 12,
"netprice": 72
}
]
}
]
productIdでグループ化しようとlist.GroupBy(g => g.ProductId)
しましたが、次に何をすべきかわかりません。可能であれば、エレガントなLINQソリューションを探しています。
このクエリを使用して、リストを新しい形式に変換できます。
var condensed =
list.GroupBy(p => p.ProductId)
.Select(g =>
{
var first = g.First();
return new
{
first.BranchName,
first.BranchId,
first.ProductId,
first.ProductName,
first.Bundle,
fees = g.Select(p => new
{
p.CostType,
p.FeeId,
p.FeeName,
p.DiscountType,
p.DiscountAmount,
p.BasePrice,
p.GrossPrice,
p.VatRate,
p.VatAmount,
p.NetPrice
})
.ToList()
};
})
.ToList();
これが実際のデモです:https://dotnetfiddle.net/tweKHY
この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。
侵害の場合は、連絡してください[email protected]
コメントを追加