GroupBy分组的运用和linq左连接

时间:2022-04-11 09:41:41

最简单的分组

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 ? : bje.payTotalMoney,
bjfTotalRefund = bjte == null ? : bjte.payTotalMoney,
xpxTotalReceive = xpxe == null ? : xpxe.payTotalMoney,
xpxTotalRefund = xpxte == null ? : xpxte.payTotalMoney,
cfTotalReceive = cfe == null ? : cfe.payTotalMoney,
cfTotalRefund = cfte == null ? : cfte.payTotalMoney,
xyglfTotalReceive = xye == null ? : xye.payTotalMoney,
xyglfTotalRefund = xyte == null ? : xyte.payTotalMoney,
yjTotalReceive = yje == null ? : yje.payTotalMoney,
yjTotalRefund = yjte == null ? : yjte.payTotalMoney,
zdyTotalReceive = zdye == null ? : zdye.payTotalMoney,
zdyTotalRefund = zdyte == null ? : zdyte.payTotalMoney,
childTotalReceive = ((bje == null ? : bje.payTotalMoney) + (xpxe == null ? : xpxe.payTotalMoney) + (cfe == null ? : cfe.payTotalMoney) + (xye == null ? : xye.payTotalMoney) + (yje == null ? : yje.payTotalMoney) + (zdye == null ? : zdye.payTotalMoney)),
childTotalRefund = ((bjte == null ? : bjte.payTotalMoney) + (xpxte == null ? : xpxte.payTotalMoney) + (cfte == null ? : cfte.payTotalMoney) + (xyte == null ? : xyte.payTotalMoney) + (yjte == null ? : yjte.payTotalMoney) + (zdyte == null ? : zdyte.payTotalMoney)),
});