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 UserName FROM UserInfo AS T2 WHERE T2.Id=T1.UserId) AS UserName, SUM(T1.RealTotal) AS RealTotal FROM Orders AS T1

WHERE 1=1 AND T1.SalesUserId=131 AND T1.PayStatusId=2

AND (T1.PayTime>=‘2017-05-01 00:00:00‘ AND T1.PayTime<=‘2017-05-31 23:59:59‘)

GROUP BY T1.UserId

) AS T

2)   LINQ 实现

var query = (from t1 in DataContext.Orders

where t1.SalesUserId == salesUserId && (t1.PayTime >= mbdt && t1.PayTime <= medt)

group t1 by t1.UserId into g1

select new

{

TradeName = (from t2 in DataContext.UserInfo

where t2.id == g1.Key

select t2.TradeName).FirstOrDefault(),

UserName = (from t2 in DataContext.UserInfo

where t2.id == g1.Key

select t2.userName).FirstOrDefault(),

RealTotal = g1.Sum(o => o.RealTotal)

}

into v1

orderby v1.RealTotal descending

select v1).AsEnumerable().Select((o, i) =>

new CustomOrderAmountRankingModel

{

Sn = i + 1,

CustomerShopName = o.TradeName,

RegisterUserName = o.UserName,

OrderAmount = o.RealTotal

});

3)   生成SQL

exec sp_executesql N‘SELECT

[Project10].[C1] AS [C1],

[Project10].[C2] AS [C2],

[Project10].[C3] AS [C3],

[Project10].[C4] AS [C4]

FROM ( SELECT

1 AS [C1],

[Project9].[C1] AS [C2],

[Project9].[C2] AS [C3],

[Project9].[C3] AS [C4]

FROM ( SELECT

[Project8].[C1] AS [C1],

[Project8].[C2] AS [C2],

(SELECT

SUM([Extent4].[RealTotal]) AS [A1]

FROM [dbo].[Orders] AS [Extent4]

WHERE ([Extent4].[SalesUserId] = @p__linq__0) AND ([Extent4].[PayTime] >= @p__linq__1) AND ([Extent4].[PayTime] <= @p__linq__2) AND ([Project8].[UserId] = [Extent4].[UserId])) AS [C3]

FROM ( SELECT

[Project7].[UserId] AS [UserId],

[Project7].[C1] AS [C1],

[Project7].[C2] AS [C2]

FROM ( SELECT

[Project5].[UserId] AS [UserId],

[Project5].[C1] AS [C1],

(SELECT TOP (1)

[Extent3].[userName] AS [userName]

FROM [dbo].[UserInfo] AS [Extent3]

WHERE [Extent3].[id] = [Project5].[UserId]) AS [C2]

FROM ( SELECT

[Project4].[UserId] AS [UserId],

[Project4].[C1] AS [C1]

FROM ( SELECT

[Project2].[UserId] AS [UserId],

(SELECT TOP (1)

[Extent2].[TradeName] AS [TradeName]

FROM [dbo].[UserInfo] AS [Extent2]

WHERE [Extent2].[id] = [Project2].[UserId]) AS [C1]

FROM ( SELECT

[Distinct1].[UserId] AS [UserId]

FROM ( SELECT DISTINCT

[Extent1].[UserId] AS [UserId]

FROM [dbo].[Orders] AS [Extent1]

WHERE ([Extent1].[SalesUserId] = @p__linq__0) AND ([Extent1].[PayTime] >= @p__linq__1) AND ([Extent1].[PayTime] <= @p__linq__2)

)  AS [Distinct1]

)  AS [Project2]

)  AS [Project4]

)  AS [Project5]

)  AS [Project7]

)  AS [Project8]

)  AS [Project9]

)  AS [Project10]

ORDER BY [Project10].[C4] DESC‘,N‘@p__linq__0 bigint,@p__linq__1 datetime2(7),@p__linq__2 datetime2(7)‘,@p__linq__0=131,@p__linq__1=‘2017-05-01 00:00:00‘,@p__linq__2=‘2017-05-31 23:59:59‘

4)   结果集(两种实现方式相同)

?  总结:可以看出 LINQ 生成的查询语句中,在子查询中使用 WHERE + DICTINCT 实现,并没有GROUP BY。两者执行耗时也没有什么区别,只是 LINQ 使用了过多的派生表,增加了理解的难度性。

时间: 2024-11-03 01:18:58

LINQ to SQL 实现 GROUP BY、聚合、ORDER 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/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

SQL的GROUP BY 与 Order By

1.概述 “Group By”从字面意义上理解就是根据“By”指定的规则对数据进行分组,所谓的分组就是将一个“数据集”划分成若干个“小区域”,然后针对若干个“小区域”进行数据处理. 2.原始表 3.简单Group By 示例1 select 类别, sum(数量) as 数量之和 from A group by 类别 返回结果如下表,实际上就是分类汇总. 4.Group By 和 Order By 示例2 select 类别, sum(数量) AS 数量之和 from A group by 类别

LINQ to SQL 模拟实现 ROW_NUMBER() OVER(ORDER BY ...) 的功能

?  前言 本来是想使用 LINQ 实现类似 SQL: ROW_NUMBER() OVER(ORDER BY -) 的功能,但是貌似 LINQ 不支持,反正没找到解决办法,无奈使用了LINQ Select() 方法实现. 1)   需求,需要实现一下 SQL: SELECT TOP 10 ROW_NUMBER() OVER(ORDER BY T.TotalAmount DESC) AS SN, * FROM ( SELECT T2.Name, SUM(T2.Amount) AS TotalAmo

LINQ to SQL语句(5)之Order By

Order By操作 适用场景:对查询出的语句进行排序,比如按时间排序 等等. 说明:按指定表达式对集合排序:延迟,:按指定表达式对集合 排序:延迟,默认是升序,加上descending表示降序,对应的扩展方法是 OrderBy和OrderByDescending 1.简单形式 这个例子使用 orderby 按雇用日期对雇员进行排序: var q = from e in db.Employees orderby e.HireDate select e; 说明:默认为升序 2.带条件形式 注意:W

LINQ体验(7)——LINQ to SQL语句之Group By/Having和Exists/In/Any/All/Contains

我们继续讲解LINQ to SQL语句,这篇我们来讨论Group By/Having操作符和Exists/In/Any/All/Contains操作符. Group By/Having操作符 适用场景:分组数据,为我们查找数据缩小范围. 说明:分配并返回对传入参数进行分组操作后的可枚举对象.分组:延迟 1.简单形式: var q = from p in db.Products group p by p.CategoryID into g select g; 语句描述:使用Group By按Cat

Linq to SQL -- Group By、Having和Exists、In、Any、All、Contains

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