.NET 中使用 LINQ 可以方便地进行多列分组操作。本文主要介绍.NET(C#)中,使用Linq时实现对多列数据进行分组的方法及示例代码。

1、C# 3或更高版本使用匿名类型

// 声明式查询语法
var result3 = 
    from x in table
    group x by new { x.Column1, x.Column2 } into g
    select new { g.Key.Column1, g.Key.Column2, QuantitySum = g.Sum(x => x.Quantity) };
// /或方法的语法
var result4 = table.GroupBy(x => new { x.Column1, x.Column2 })
    .Select(g => 
      new { g.Key.Column1, g.Key.Column2 , QuantitySum= g.Sum(x => x.Quantity) });

使用示例:

using System;
using System.Collections.Generic;
using System.Linq;

public class Order
{
    public string Category { get; set; }
    public string Supplier { get; set; }
    public int Quantity { get; set; }
}

public class Program
{
    public static void Main()
    {
        // 创建订单数据列表
        List<Order> orders = new List<Order>
        {
            new Order { Category = "Electronics", Supplier = "SupplierA", Quantity = 100 },
            new Order { Category = "Electronics", Supplier = "SupplierB", Quantity = 150 },
            new Order { Category = "Furniture", Supplier = "SupplierA", Quantity = 200 },
            new Order { Category = "Furniture", Supplier = "SupplierB", Quantity = 50 },
            new Order { Category = "Electronics", Supplier = "SupplierA", Quantity = 75 }
        };

        // 使用声明式查询语法进行分组和汇总
        var result3 =
            from x in orders
            group x by new { x.Category, x.Supplier } into g
            select new 
            { 
                g.Key.Category, 
                g.Key.Supplier, 
                QuantitySum = g.Sum(x => x.Quantity) 
            };

        // 输出声明式查询结果
        Console.WriteLine("结果 (声明式查询语法):");
        foreach (var group in result3)
        {
            Console.WriteLine($"Category: {group.Category}, Supplier: {group.Supplier}, QuantitySum: {group.QuantitySum}");
        }

        // 使用方法语法进行分组和汇总
        var result4 = orders.GroupBy(x => new { x.Category, x.Supplier })
            .Select(g => 
                new 
                { 
                    g.Key.Category, 
                    g.Key.Supplier, 
                    QuantitySum = g.Sum(x => x.Quantity) 
                });

        // 输出方法语法结果
        Console.WriteLine("\n结果 (方法语法):");
        foreach (var group in result4)
        {
            Console.WriteLine($"Category: {group.Category}, Supplier: {group.Supplier}, QuantitySum: {group.QuantitySum}");
        }
    }
}

2、C# 7.1或更高版本使用元组和推断的元组元素名

// 声明式查询语法
var result = 
    from x in table
    group x by (x.Column1, x.Column2) into g
    select (g.Key.Column1, g.Key.Column2, QuantitySum: g.Sum(x => x.Quantity));
//或方法的语法
var result2 = table.GroupBy(x => (x.Column1, x.Column2))
    .Select(g => (g.Key.Column1, g.Key.Column2, QuantitySum: g.Sum(x => x.Quantity)));

使用示例:

using System;
using System.Collections.Generic;
using System.Linq;

public class Order
{
    public string Column1 { get; set; }
    public string Column2 { get; set; }
    public int Quantity { get; set; }
}

public class Program
{
    public static void Main()
    {
        // 创建订单数据列表
        List<Order> table = new List<Order>
        {
            new Order { Column1 = "Electronics", Column2 = "SupplierA", Quantity = 100 },
            new Order { Column1 = "Electronics", Column2 = "SupplierB", Quantity = 150 },
            new Order { Column1 = "Furniture", Column2 = "SupplierA", Quantity = 200 },
            new Order { Column1 = "Furniture", Column2 = "SupplierB", Quantity = 50 },
            new Order { Column1 = "Electronics", Column2 = "SupplierA", Quantity = 75 }
        };

        // 使用声明式查询语法进行分组和汇总
        var result = 
            from x in table
            group x by new { x.Column1, x.Column2 } into g
            select new { g.Key.Column1, g.Key.Column2, QuantitySum = g.Sum(x => x.Quantity) };

        // 输出声明式查询结果
        Console.WriteLine("结果 (声明式查询语法):");
        foreach (var group in result)
        {
            Console.WriteLine($"Column1: {group.Column1}, Column2: {group.Column2}, QuantitySum: {group.QuantitySum}");
        }

        // 使用方法语法进行分组和汇总
        var result2 = table
            .GroupBy(x => new { x.Column1, x.Column2 })
            .Select(g => new { g.Key.Column1, g.Key.Column2, QuantitySum = g.Sum(x => x.Quantity) });

        // 输出方法语法结果
        Console.WriteLine("\n结果 (方法语法):");
        foreach (var group in result2)
        {
            Console.WriteLine($"Column1: {group.Column1}, Column2: {group.Column2}, QuantitySum: {group.QuantitySum}");
        }
    }
}

推荐文档