Ø  前言

本示例主要实现 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. 无法定位程序输入点 zend_empty_string php7.dll

    phpstudy 在安装php_redis.dll php_redis.pdb 时,需要用到php_igbinary.dll扩展.但我下载的版本不对.下载的是7.2版本的. 所以报以上错误.可选择版本 ...

  2. 使用 py.test 对 python 代码进行测试

    其实以前我记得有人对我说过,写代码甚至可以先写完测试之后部署好了再开始写逻辑代码.我觉得有点吃惊和奇怪,这不是扯淡吗? 但是这次在完成了积分支付第一阶段开发之后我意识到,这可能并不是开玩笑,特别是项目 ...

  3. 用Delphi制作动态菜单 该文章《用Delphi制作动态菜单》

    ---恢复内容开始--- 1.首先,确定动态菜单的数据来源,即要确定动态菜单标题是来自Windows的系统注册表,还是来自一个数据库,或者是来自一个子目录,主要由程序的功能而定.这里假设主窗口名为Ma ...

  4. python之count()函数

    # count()统计字符串中特定单词或短语出现次数(n = 3) strs = 'Good! Today is good day! Good job!' n = strs.lower().count ...

  5. BZOJ2557[Poi2011]Programming Contest——匈牙利算法+模拟费用流

    题目描述 Bartie and his friends compete in the Team Programming Contest. There are n contestants on each ...

  6. windows service 安装/卸载

    第一种方法: 前提: Service1 中的serviceProcessInstaller1设置 Account为localSystem 1. 开始 ->运行 ->cmd(管理员身份运行) ...

  7. 【 Gym 101116K 】Mixing Bowls(dfs)

    BUPT2017 wintertraining(15) #4H Gym - 101116K 题意 给定一个菜谱,大写的单词代表混合物,小写的代表基础原料.每个混合物由其它混合物或基础原料组成,不会间接 ...

  8. 【POI每日题解 #8】DYN-Dynamite

    你问蒟蒻为什么一天写两篇每日题解? 难道每日坚果你不能一天吃两包吗? 题目链接 哇…这道题第一反应就是二分答案[太明显了 枚举答案 就那个“关键节点到这些点中距离的最小值的最大值”[蒟蒻读了好几遍…… ...

  9. 04 自学Aruba之定制AC的protal认证登陆页面

    点击返回:自学Aruba之路 04 自学Aruba之定制AC的protal认证登陆页面 方法一: 使用Aruba控制器中内置的网页界面 Configuration下MANAGEMENT>Capt ...

  10. 16 利用Zabbix完成windows监控

    点击返回:自学Zabbix之路 16 利用Zabbix完成windows监控 1.安装zabbix_agentd 1.1.下载zabbix_agentd监控客户端软件安装包(windows操作系统客户 ...