Ø  前言

本示例主要实现 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 使用了过多的派生表,增加了理解的难度性。

LINQ to SQL 实现 GROUP BY、聚合、ORDER BY的更多相关文章

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

    Linq to sql 使用group by 统计多个字段,然后返回多个字段的值,话不多说,直接上例子: where u.fy_no == fy_no orderby u.we_no group u  ...

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

    Group By/Having操作符 适用场景:分组数据,为我们查找数据缩小范围. 说明:分配并返回对传入参数进行分组操作后的可枚举对象.分组:延迟 1.简单形式: var q = from p in ...

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

    http://wenku.baidu.com/link?url=2RsCun4Mum1SLbh-LHYZpTmGFMiEukrWAoJGKGpkiHKHeafJcx2y-HVttNMb1BqJpNdw ...

  4. [转]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操作符 适用场景:分组数据, ...

  5. SQL的GROUP BY 与 Order By

    1.概述 “Group By”从字面意义上理解就是根据“By”指定的规则对数据进行分组,所谓的分组就是将一个“数据集”划分成若干个“小区域”,然后针对若干个“小区域”进行数据处理. 2.原始表 3.简 ...

  6. SQL语句group by 与order by 执行顺序引发的一场“内斗”

    直入主题!看看下面这SQL会不会报错?如果报错应该是什么错误! --说明:黑色字体都是列SELECT application_id, index_num, num, amount FROM `cred ...

  7. LINQ to SQL语句之Join和Order By

    Join操作 适用场景:在我们表关系中有一对一关系,一对多关系,多对多关系等.对各个表之间的关系,就用这些实现对多个表的操作. 说明:在Join操作中,分别为Join(Join查询), SelectM ...

  8. LINQ体验(6)——LINQ to SQL语句之Join和Order By

    Join操作 适用场景:在我们表关系中有一对一关系,一对多关系.多对多关系等.对各个表之间的关系,就用这些实现对多个表的操作. 说明:在Join操作中.分别为Join(Join查询), SelectM ...

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

    Ø  前言 本来是想使用 LINQ 实现类似 SQL: ROW_NUMBER() OVER(ORDER BY -) 的功能,但是貌似 LINQ 不支持,反正没找到解决办法,无奈使用了LINQ Sele ...

随机推荐

  1. FuelPHP 系列(五) ------ Security 防御

    项目中难免会有 form 提交,对用户输入的所有信息进行过滤,可以避免 XSS 攻击,防止 SQL 注入. 一.设置配置信息 首先在 config.php 文件中,对 security 相关信息进行设 ...

  2. GlusterFS 增删节点及改变复制份数

    一.增加节点 1.需要主机添加到主机池中 gluster peer  probe server3 gluster peer  probe server4 2.查看状态 3.添加节点并复制2份(增加复制 ...

  3. js regex variable & Set, Map

    js regex variable & Set, Map regex, variable, Set, Map, 交集, 差集, 并集, https://stackoverflow.com/qu ...

  4. C# 语言习惯

    目录 一.使用属性而不是可访问的数据成员 二.使用运行时常量(readonly)而不是编译时常量(const) 三.推荐使用 is 或 as 操作符而不是强制类型转换 四.使用 Conditional ...

  5. Spark_RDD之简单Java函数接口

    函数名 实现的方法 用途 Function<T, R> R call(T) 接收一个输入值并返回一个输出值,用于类似 map() 和filter() 等操作中 Function2<T ...

  6. BZOJ3545&3551[ONTAK2010]Peaks——kruskal重构树+主席树+dfs序+树上倍增

    题目描述 在Bytemountains有N座山峰,每座山峰有他的高度h_i.有些山峰之间有双向道路相连,共M条路径,每条路径有一个困难值,这个值越大表示越难走,现在有Q组询问,每组询问询问从点v开始只 ...

  7. BZOJ3876 AHOI/JSOI2014支线剧情(上下界网络流)

    原图所有边下界设为1上界设为inf花费为时间,那么显然就是一个上下界最小费用流了.做法与可行流类似. 因为每次选的都是最短路增广,且显然不会有负权增广路,所以所求出来的可行流的费用就是最小的. #in ...

  8. hdu 3949 XOR (线性基)

    链接: http://acm.hdu.edu.cn/showproblem.php?pid=3949 题意: 给出n个数,从中任意取几个数字异或,求第k小的异或和 思路: 线性基求第k小异或和,因为题 ...

  9. source、sh、bash

    source.sh.bash../ 执行脚本的区别 1.source命令用法: source FileName 作用:在当前bash环境下读取并执行FileName中的命令.该filename文件可以 ...

  10. luogu4932 浏览器 (拆)

    分析1的个数的奇偶性: 奇xor奇=偶xor偶=偶 奇xor偶=奇 所以只要统计1的个数是奇数的数的个数 和 是偶数的个数 乘一起就行了 直接用bitset来做,虽然常数很小/数据随机可以过,但复杂度 ...