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();
}

时间: 2024-10-13 04:16:43

Linq 多个实体Group by的相关文章

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

Linq to sql 使用group by 统计多个字段,然后返回多个字段的值,话不多说,直接上例子: var wflist = from u in db.TWelFare where u.fy_no == fy_no orderby u.we_no group u by new { weno = u.we_no, wename = u.we_name } into g select new { g.Key.weno, g.Key.wename }; 结果就是根据we_no和we_name的统

linq to sql (Group By/Having/Count/Sum/Min/Max/Avg操作符) (转帖)

http://wenku.baidu.com/link?url=2RsCun4Mum1SLbh-LHYZpTmGFMiEukrWAoJGKGpkiHKHeafJcx2y-HVttNMb1BqJpNdwaOpCflaajFY6k36IoCH_D82bk2ccu468uzDRXvG 基于LINQ+to+Entity数据访问技术的应用研究 Group By/Having操作符 适用场景:分组数据,为我们查找数据缩小范围. 说明:分配并返回对传入参数进行分组操作后的可枚举对象.分组:延迟 1.简单形式:

LINQ to SQL 实现 GROUP BY、聚合、ORDER BY

?  前言 本示例主要实现 LINQ 查询,先分组,再聚合,最后在排序.示例很简单,但是使用 LINQ 却生成了不同的 SQL 实现. 1)   采用手动编写 SQL 实现 SELECT ROW_NUMBER() OVER(ORDER BY T.RealTotal DESC) AS SN, * FROM ( SELECT (SELECT TradeName FROM UserInfo AS T2 WHERE T2.Id=T1.UserId) AS TradeName, (SELECT UserN

linq to sql (Group By/Having/Count/Sum/Min/Max/Avg操作符)

Group By/Having操作符 适用场景:分组数据,为我们查找数据缩小范围. 说明:分配并返回对传入参数进行分组操作后的可枚举对象.分组:延迟 1.简单形式: var q = from p in db.Products group p by p.CategoryID into g select g; 语句描述:使用Group By按CategoryID划分产品. 说明:from p in db.Products 表示从表中将产品对象取出来.group p by p.CategoryID i

[转]linq to sql (Group By/Having/Count/Sum/Min/Max/Avg操作符)

本文转自:http://www.cnblogs.com/jack-liang/archive/2011/03/22/1991554.html Group By/Having操作符 适用场景:分组数据,为我们查找数据缩小范围. 说明:分配并返回对传入参数进行分组操作后的可枚举对象.分组:延迟 1.简单形式: var q = from p in db.Products group p by p.CategoryID into g select g; 语句描述:使用Group By按CategoryI

(转)C#用Linq实现DataTable的Group by数据统计

本文转载自:http://www.cnblogs.com/sydeveloper/archive/2013/03/29/2988669.html 1.用两层循环计算,前提条件是数据已经按分组的列排好序的. DataTable dt = new DataTable(); dt.Columns.AddRange(new DataColumn[] { new DataColumn("name", typeof(string)), new DataColumn("sex",

linq和ef关于group by取最大值的两种写法

LINQ: var temp = from p in db.jj_Credentials group p by p.ProfessionID into g select new { g.Key, MaxPrice = g.Max(p => p.CredentialsRankID) }; EF: var temp1 = db.jj_Credentials.GroupBy(m => m.ProfessionID).Select(m => m.Max(o => o.Credentials

Linq使用小记之Group By

private void cmbStore_SelectedIndexChanged(object sender, EventArgs e) { DataTable vDt = ParamClass.ChargDt; var q = from b in vDt.AsEnumerable() group b by new { storeid = b["STOREID"], charg = b["charg"] } into g select new { g.Key.s

LinQ to SQL 查询

LINQ to SQL 是将对象关系映射到.NET框架中的一种实现.它可以将关系数据库映射为.NET Framework中的一些类. 然后,开发人员就可以通过使用 LINQ to SQL对数据库中的数据进行查询.修改.插入.删除等操作. LINQ to SQL类映射到SQL Server数据库中的表,这些类被称做“实体类”,实体类中的属性或字段映射到SQL server 数据库中表的列,实体类之间的关联映射到 SQL Server数据库中的外键关系,LINQ to SQL类中的方法映射为SQL