簡體   English   中英

將兩個列表項連接為一項

[英]Concatenate two list items into one item

我正在嘗試根據productId連接列表項。 為 JSON 命名道歉,我知道這不是最好的,我無法控制。

如果我的列表中的項目具有相同的 productId,我想將它們連接起來並創建一個包含變量屬性的費用數組的項目,即costTypefeeIdfeeName等(請參閱所需的結果)。 當 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

要生成類似於您想要的 output - 並在此過程中做出一些假設 - 嘗試這樣的事情:

list.GroupBy(item => item.ProductId /*, <your value selector lambda goes here which only returns a tuple composed of what goes as children of "fee" element> */).Select(g => 
{
    var item = g.First();
    return new { item.BranchName, item.BranchId, item.ProductId, item.ProductName, item.Bundle };
});

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM