1. 程式人生 > >C#,List集合多重GroupBy

C#,List集合多重GroupBy

pre 效果 ide color bubuko sum req clr mon

1 原始數據

技術分享圖片

2 寫法

var data1 = _m.GetPurchase_Title(MasterId).FirstOrDefault();//表頭
                var data2 = _m.GetPurchaseDt(MasterId);//明細

                var result = new
                {
                    PurNo = data1.MasterId,
                    ProviderName = data1.ProvideName,
                    Remark 
= data1.Remark, SumCount = data1.preQty, SumMoney = data1.OMoney, Detail_style = data2.GroupBy(m => m.style).Select(g => new { style = g.Key, Detail_Sku = g.Select(n => n) .GroupBy(x
=> new { x.StyleName, x.SerialNo, x.AttribName, x.ClrName, x.Oprice }) .Select(t => new { preQty = t.Sum(b => b.preQty), OPrice = t.Key.Oprice, OMoney = t.Sum(b => b.OMoney), StyleName
= t.Key.StyleName, SerialNo = t.Key.SerialNo, AttribName = t.Key.AttribName, ClrName = t.Key.ClrName, Size = t.Select(v => v).GroupBy(z => z.SizeName).Select(k => k.Key + ":" + k.Sum(l => l.preQty).ToString()) }) }) };

3 效果:

{
            "errmsg": "ok",
            "PurNo": "B201709270000019",
            "ProviderName": "外4Q-37615林誌峰",
            "Remark": "這是一段牛逼的備註",
            "SumCount": 100,
            "SumMoney": 2400.00,
            "Detail_style": [{
                    "style": "10B73A33980",
                    "Detail_Sku": [{
                        "preQty": 8,
                        "OPrice": 100.00,
                        "OMoney": 2400.00,
                        "StyleName": "這是品名",
                        "SerialNo": "原廠編號",
                        "AttribName": "專櫃",
                        "ClrName": "大紅",
                        "SizebName": ["36:10", "37:10", "38:20", "L:30", "XLL:100"]
                    }, {
                        "SkuCount": 9,
                        "OPrice": 100.00,
                        "OMoney": 2400.00,
                        "StyleName": "這是品名",
                        "SerialNo": "原廠編號",
                        "AttribName": "專櫃",
                        "ClrName": "",
                        "SizebName": ["X:10", "XX:10", "XXL:20", "L:30", "XLL:100"]
                    }]
                },
                {
                    "style": "20B73A33981",
                    "Detail_Sku": [{
                        "SkuCount": 10,
                        "OPrice": 100.00,
                        "OMoney": 2400.00,
                        "StyleName": "這是品名",
                        "SerialNo": "原廠編號",
                        "AttribName": "專櫃",
                        "ClrName": "顏色",
                        "SizebName": ["X:10", "XX:10", "XXL:20", "L:30", "XLL:100"]
                    }, {
                        "SkuCount": 11,
                        "OPrice": 100.00,
                        "OMoney": 2400.00,
                        "StyleName": "這是品名",
                        "SerialNo": "原廠編號",
                        "AttribName": "專櫃",
                        "ClrName": "顏色",
                        "SizebName": ["X:10", "XX:10", "XXL:20", "L:30", "XLL:100"]
                    }]
                }
            ]
        }

C#,List集合多重GroupBy