Linq 多个实体Group by

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Linq 多个实体Group by相关的知识,希望对你有一定的参考价值。

自产自销,不喜欢匆喷

解决思路,搞不定的地方就用new 加{}

 

demo

using (var db = DbServices.GetDbProvider())
{
IQueryable<AreaPropertyLogEntity> queryAreaProperty = db.GetQuery<AreaPropertyLogEntity>();
IQueryable<TransferEntity> queryTransfer = db.GetQuery<TransferEntity>();
var query = (from areaPropery in queryAreaProperty
join transfer in queryTransfer on areaPropery.TransferCode equals transfer.TransferCode
where areaPropery.PropId == propId
group new { transfer, areaPropery }
by new
{
ID = areaPropery.ID,
TransferCode = transfer.TransferName,
AreaCode = areaPropery.AreaCode,
AreaName = areaPropery.AreaName,
CanCOD = areaPropery.CanCOD,
CanMovePos = areaPropery.CanMovePos,
CanCWO = areaPropery.CanCWO,
SchemeCode = areaPropery.SchemeCode,
CreateTime = areaPropery.CreateTime,
Creator = areaPropery.Creator,
CreatorID = areaPropery.CreatorID
}

into g
select new AreaPropertyLogEntity
{
ID = g.Key.ID,
TransferCode = g.Key.TransferCode,
AreaCode = g.Key.AreaCode,
AreaName = g.Key.AreaName,
CanCOD = g.Key.CanCOD,
CanMovePos = g.Key.CanMovePos,
CanCWO = g.Key.CanCWO,
SchemeCode = g.Key.SchemeCode,
CreateTime = g.Key.CreateTime,
Creator = g.Key.Creator,
CreatorID = g.Key.CreatorID
}


).ToList();
}

以上是关于Linq 多个实体Group by的主要内容,如果未能解决你的问题,请参考以下文章

C# Linq group by 和 group by into 运用实例

无法使用 Nhibernate 的 Linq 检索 Group By 实体或复合键

C# Linq to sql 实现 group by 统计多字段 返回多字段

LINQ Group By并将Group的子列表合并回唯一列表

带有表连接、case 语句、计数、group by 子句的 Linq 查询

3-实体数据模型与LINQ-分组