码迷,mamicode.com
首页 > Windows程序 > 详细

C#,List集合多重GroupBy

时间:2018-05-11 20:11:17      阅读:547      评论:0      收藏:0      [点我收藏+]

标签:var   图片   col   div   data   purchase   fir   tle   err   

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

标签:var   图片   col   div   data   purchase   fir   tle   err   

原文地址:https://www.cnblogs.com/zoumin123/p/9025963.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!