LINQ to SQL 实现 GROUP BY聚合ORDER BY

Posted Abeam

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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

                  

以上是关于LINQ to SQL 实现 GROUP BY聚合ORDER BY的主要内容,如果未能解决你的问题,请参考以下文章

C# Linq to SQL — Group by

LINQ to SQL语句之Group By/Having

LINQ to Sql 左外连接与 Group By 和 Have 子句

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

将包含 where、group by、sum 和必须的 MySQL 语句转换为 Linq to Sql

EF Linq to Sql 多表left join查询并对结果group by分组之后进行count,max等处理

(c)2006-2024 SYSTEM All Rights Reserved IT常识