LINQ系列:LINQ to SQL Group by/Having分组

1. 简单形式

var expr = from p in context.Products
           group p by p.CategoryID into g
           select g;

foreach (var item in expr)
{
    Console.WriteLine(item.Key);

    foreach (var p in item)
    {
        Console.WriteLine("{0}-{1}", p.ProductID, p.ProductName);
    }
}
SELECT
    [Project2].[CategoryID] AS [CategoryID],
    [Project2].[C1] AS [C1],
    [Project2].[ProductID] AS [ProductID],
    [Project2].[CategoryID1] AS [CategoryID1],
    [Project2].[ProductName] AS [ProductName],
    [Project2].[UnitPrice] AS [UnitPrice],
    [Project2].[UnitsInStock] AS [UnitsInStock],
    [Project2].[Discontinued] AS [Discontinued]
    FROM ( SELECT
        [Distinct1].[CategoryID] AS [CategoryID],
        [Extent2].[ProductID] AS [ProductID],
        [Extent2].[CategoryID] AS [CategoryID1],
        [Extent2].[ProductName] AS [ProductName],
        [Extent2].[UnitPrice] AS [UnitPrice],
        [Extent2].[UnitsInStock] AS [UnitsInStock],
        [Extent2].[Discontinued] AS [Discontinued],
        CASE WHEN ([Extent2].[ProductID] IS NULL) THEN CAST(NULL AS int) ELSE 1 END AS [C1]
        FROM   (SELECT DISTINCT
            [Extent1].[CategoryID] AS [CategoryID]
            FROM [dbo].[Product] AS [Extent1] ) AS [Distinct1]
        LEFT OUTER JOIN [dbo].[Product] AS [Extent2] ON [Distinct1].[CategoryID] = [Extent2].[CategoryID]
    )  AS [Project2]
    ORDER BY [Project2].[CategoryID] ASC, [Project2].[C1] ASC

2. 最大值

var expr = from p in context.Products
            group p by p.CategoryID into g
            select new
            {
                g.Key,
                MaxUnitPrice = g.Max(p => p.UnitPrice)
            };

foreach (var item in expr)
{
    Console.WriteLine("{0}-{1}", item.Key, item.MaxUnitPrice);
}
SELECT
    [GroupBy1].[K1] AS [CategoryID],
    [GroupBy1].[A1] AS [C1]
    FROM ( SELECT
        [Extent1].[CategoryID] AS [K1],
        MAX([Extent1].[UnitPrice]) AS [A1]
        FROM [dbo].[Product] AS [Extent1]
        GROUP BY [Extent1].[CategoryID]
    )  AS [GroupBy1]

3. 最小值

var expr = from p in context.Products
            group p by p.CategoryID into g
            select new
            {
                g.Key,
                MinUnitPrice = g.Min(p => p.UnitPrice)
            };
SELECT
    [GroupBy1].[K1] AS [CategoryID],
    [GroupBy1].[A1] AS [C1]
    FROM ( SELECT
        [Extent1].[CategoryID] AS [K1],
        MIN([Extent1].[UnitPrice]) AS [A1]
        FROM [dbo].[Product] AS [Extent1]
        GROUP BY [Extent1].[CategoryID]
    )  AS [GroupBy1]

4. 平均值

var expr = from p in context.Products
            group p by p.CategoryID into g
            select new
            {
                g.Key,
                AverageUnitPrice = g.Average(p => p.UnitPrice)
            };
SELECT
    [GroupBy1].[K1] AS [CategoryID],
    [GroupBy1].[A1] AS [C1]
    FROM ( SELECT
        [Extent1].[CategoryID] AS [K1],
        AVG([Extent1].[UnitPrice]) AS [A1]
        FROM [dbo].[Product] AS [Extent1]
        GROUP BY [Extent1].[CategoryID]
    )  AS [GroupBy1]

5. 求和

var expr = from p in context.Products
            group p by p.CategoryID into g
            select new
            {
                g.Key,
                TotalUnitPrice = g.Sum(p => p.UnitPrice)
            };
SELECT
    [GroupBy1].[K1] AS [CategoryID],
    [GroupBy1].[A1] AS [C1]
    FROM ( SELECT
        [Extent1].[CategoryID] AS [K1],
        SUM([Extent1].[UnitPrice]) AS [A1]
        FROM [dbo].[Product] AS [Extent1]
        GROUP BY [Extent1].[CategoryID]
    )  AS [GroupBy1]

6. 计数

var expr = from p in context.Products
            group p by p.CategoryID into g
            select new
            {
                g.Key,
                ProductNumber = g.Count()
            };
SELECT
    [GroupBy1].[K1] AS [CategoryID],
    [GroupBy1].[A1] AS [C1]
    FROM ( SELECT
        [Extent1].[CategoryID] AS [K1],
        COUNT(1) AS [A1]
        FROM [dbo].[Product] AS [Extent1]
        GROUP BY [Extent1].[CategoryID]
    )  AS [GroupBy1]
var expr = from p in context.Products
            group p by p.CategoryID into g
            select new
            {
                g.Key,
                ProductNumber = g.Count(p => p.UnitsInStock > 0)
            };
时间: 2024-10-13 19:40:21

LINQ系列:LINQ to SQL Group by/Having分组的相关文章

LINQ TO SQL ——Group by

原文:LINQ TO SQL --Group by 分组在SQL中应用的十分普遍,在查询,统计时都有可能会用到它.LINQ TO SQL中同样具备group的功能,这篇我来讲下LINQ TO SQL中关于group的用法. 说明: 1:group 不一定要用到多表查询中,单表查询也是可以的. 2:基本语法: 1>:group 分组后要取的字段或者是对象; 2>:by 分组对象,可以是一列也可以是多列: 3>:into 结果集对象. 3:group的分类,这里分为单列分组,和多列分组.  

Linq语法详细(三种方式:linq、Lambda、SQL语法)

三种方式:linq.Lambda.SQL语法 1.简单的linq语法 //1 var ss = from r in db.Am_recProScheme select r; //2 var ss1 = db.Am_recProScheme; //3 string sssql = "select * from Am_recProScheme"; 2.带where的查询 //1 var ss = from r in db.Am_recProScheme where r.rpId >

Linq之Linq to Sql

目录 写在前面 系列文章 Linq to sql 总结 写在前面 上篇文章介绍了linq to xml的相关内容,linq to xml提供一种更便捷的创建xml树,及查询的途径.这篇文章将继续介绍linq to sql的内容.个人觉得linq to sql在实际开发中在中小型项目中用的比较多,在中小型项目用ef或者nhibernate这些orm确实有点重量级.Linq to Sql提供了丰富的功能,完全可以满足日常数据访问的需求.使用方法也非常简单.灵活. 系列文章 Linq之Lambda表达

Linq系列

LINQ 图解 Linq中的Select——投影 Linq学习资源 Expert C# 5.0中的Linq部分

Linq技术四:动态Linq技术 -- Linq.Expressions

前面介绍了Linq的三个方面应用:Linq to SQL, Linq to XML和Linq to Object,这篇介绍一下动态Linq的实现方式及应用场景. 命名空间: System.Linq; System.Linq.Expressions; 应用Linq的时候,我们都知道只需要Lambda表达式就行,但有些场景仅仅只使用Data Model的字段名操作是不够的或者不方便的. 场景1:假设我们需要拼接Where条件进行查询,一种方式可以拼接IQueryable的表达式.但我想像写SQL语句

Linq之Linq to XML

目录 写在前面 系列文章 linq to xml 总结 写在前面 在很多情况下,都可以见到使用xml的影子.例如,在 Web 上,在配置文件.Microsoft Office Word 文件(将word文档另存为xml文件,这也提供了一种通过操作xml,操作word的一种方式)以及数据库中,都可以看到 XML.而linq to xml提供了一种操作xml更便捷的方式. 系列文章 Linq之Lambda表达式初步认识 Linq之Lambda进阶 Linq之隐式类型.自动属性.初始化器.匿名类 Li

SQL GROUP BY 语句

合计函数 (比如 SUM) 常常需要添加 GROUP BY 语句. GROUP BY 语句 GROUP BY 语句用于结合合计函数,根据一个或多个列对结果集进行分组. SQL GROUP BY 语法 SELECT column_name, aggregate_function(column_name) FROM table_name WHERE column_name operator value GROUP BY column_name SQL GROUP BY 实例 我们拥有下面这个 "Or

SCCM 2012 R2实战系列之一:SQL安装

大家好,从今天开始跟大家一起分享自己学习SCCM 2012 R2的一些心得和具体的部署配置,希望能帮到大家.由于SCCM部署的步骤比较复杂,内容也比较多,所以我把SCCM整个部署过程分为以下三个章节: l SQL Server 2012 SP1的安装 l SCCM安装前的准备工作 l SCCM 主站点的部署 1.1 实验拓扑图: 下面是本次部署的一个拓扑图,所有操作系统均使用Windows Server 2012 R2 Datacenter版本 1.2 域环境的准备 微软的很多产品都是基于域环境

SQL GROUP BY 中的TOP N

一个示例表test(select * from test): id gid    age    username1 1      11     zhangsan2 1      13     zhangsan3 1      15     lisi4 1      10     wangwu5 2      20     liusanjie6 2      23     wumingshi7 1      10     hh 按gid分组取每组第一个数据的SQL: SELECT * FROM t