GroupBy分组的运用和linq左连接

最简单的分组

var conHistoryList = conHistoryData.GroupBy(g => g.personId);

就是conHistoryData是一个IQueryable<T>类型;

分组后组内排序

var conHistoryList = conHistoryData.GroupBy(g => g.personId).Select(g => g.OrderBy(c => c.addTime));

对数据分组之后在根据每一个分组内的某一个元素排序。

分组后排序返回第一个

 var conHistoryList = conHistoryData.GroupBy(g => g.personId).Select(g => g.OrderBy(c => c.addTime).FirstOrDefault()).ToList();

对数据分组之后在根据每一个分组内的某一个元素排序。排序之后在返回第一个元素,然后组成一个集合返回。这个实现的是根据分组取组内第一个元素然后重新返回一个元素列表。

使用组内元素

var dd = conHistoryData.GroupBy(g => g.personId);
                //获取每个分组的键
                foreach (var item in dd)
                {
                    string GroupByKey = item.Key;
                    //获取每个分组的内容
                    foreach (var item2 in item)
                    {
                        ContractHistoryInfor historyInfor = item2;
                    }
                }

多次分组组合

var childDetailListData = new List<ChildFactDetailInfor>();
var childDetailListG = childDetailList.GroupBy(b => b.materialCategory).Select(g => g.GroupBy(b => b.materialNum));               

childDetailList是一个List<ChildFactDetailInfor>集合。通过先分组materialCategory字段,然后在在分组内根据materialNum字段再次分组。

使用多次分组的元素

var childDetailListData = new List<ChildFactDetailInfor>();
                var childDetailListG = childDetailList.GroupBy(b => b.materialCategory).Select(g => g.GroupBy(b => b.materialNum));
                foreach (var item in childDetailListG)
                {
                    foreach (var item2 in item)
                    {
                        decimal? a = item2.Sum(x => x.materialNum);
                        decimal? c = item2.Sum(x => x.priceTotal);
                        foreach (var item3 in item2)
                        {
                            item3.materialNum = a;
                            item3.priceTotal = c;
                            childDetailListData.Add(item3);
                        }
                    }
                }

和上面使用元素差不多就是遍历层数

 补充记录一个linq左连接多表关联去除空数据示例:

     var data = (from a in childData
                            join bj in bjCostData
                            on a.id equals bj.childId
                            into bj
                            from bje in bj.DefaultIfEmpty()
                            join bjt in bjRefundData
                            on a.id equals bjt.childId
                            into bjt
                            from bjte in bjt.DefaultIfEmpty()
                                //学平险
                            join xpx in xpxCostData
                            on a.id equals xpx.childId
                            into xpx
                            from xpxe in xpx.DefaultIfEmpty()
                            join xpxt in xpxRefundData
                            on a.id equals xpxt.childId
                            into xpxt
                            from xpxte in xpxt.DefaultIfEmpty()
                                //餐费
                            join cf in cfCostData
                            on a.id equals cf.childId
                            into cf
                            from cfe in cf.DefaultIfEmpty()
                            join cft in cfRefundData
                            on a.id equals cft.childId
                            into cft
                            from cfte in cft.DefaultIfEmpty()
                                //自定义
                            join zdy in zdyCostData
                            on a.id equals zdy.childId
                            into zdy
                            from zdye in zdy.DefaultIfEmpty()
                            join zdyt in zdyRefundData
                            on a.id equals zdyt.childId
                            into zdyt
                            from zdyte in zdyt.DefaultIfEmpty()
                                //休园
                            join xy in xyCostData
                            on a.id equals xy.childId
                            into xy
                            from xye in xy.DefaultIfEmpty()
                            join xyt in xyRefundData
                            on a.id equals xyt.childId
                            into xyt
                            from xyte in xyt.DefaultIfEmpty()
                                //押金
                            join yj in yjCostData
                            on a.id equals yj.childId
                             into yj
                            from yje in yj.DefaultIfEmpty()
                            join yjt in yjRefundData
                            on a.id equals yjt.childId
                            into yjt
                            from yjte in yjt.DefaultIfEmpty()
                            select new H_ChildStatistics
                            {
                                id = a.id,
                                parkId = a.parkId,
                                parkName = a.parkName,
                                childName = a.childName,
                                childNameEng = a.childNameEng,
                                gradeNo = a.gradeNo,
                                classNo = a.classNo,
                                modifyTime = a.modifyTime,
                                bjfTotalReceive = bje == null ? 0 : bje.payTotalMoney,
                                bjfTotalRefund = bjte == null ? 0 : bjte.payTotalMoney,
                                xpxTotalReceive = xpxe == null ? 0 : xpxe.payTotalMoney,
                                xpxTotalRefund = xpxte == null ? 0 : xpxte.payTotalMoney,
                                cfTotalReceive = cfe == null ? 0 : cfe.payTotalMoney,
                                cfTotalRefund = cfte == null ? 0 : cfte.payTotalMoney,
                                xyglfTotalReceive = xye == null ? 0 : xye.payTotalMoney,
                                xyglfTotalRefund = xyte == null ? 0 : xyte.payTotalMoney,
                                yjTotalReceive = yje == null ? 0 : yje.payTotalMoney,
                                yjTotalRefund = yjte == null ? 0 : yjte.payTotalMoney,
                                zdyTotalReceive = zdye == null ? 0 : zdye.payTotalMoney,
                                zdyTotalRefund = zdyte == null ? 0 : zdyte.payTotalMoney,
                                childTotalReceive = ((bje == null ? 0 : bje.payTotalMoney) + (xpxe == null ? 0 : xpxe.payTotalMoney) + (cfe == null ? 0 : cfe.payTotalMoney) + (xye == null ? 0 : xye.payTotalMoney) + (yje == null ? 0 : yje.payTotalMoney) + (zdye == null ? 0 : zdye.payTotalMoney)),
                                childTotalRefund = ((bjte == null ? 0 : bjte.payTotalMoney) + (xpxte == null ? 0 : xpxte.payTotalMoney) + (cfte == null ? 0 : cfte.payTotalMoney) + (xyte == null ? 0 : xyte.payTotalMoney) + (yjte == null ? 0 : yjte.payTotalMoney) + (zdyte == null ? 0 : zdyte.payTotalMoney)),
                            });
View Code

 

posted @ 2018-07-13 10:31  YanBigFeg  阅读(993)  评论(3编辑  收藏  举报