0Pricing
C# Academy · 课时

GroupBy、Join、Aggregate 与投影(匿名类型)

对项目分组、连接序列、使用 Aggregate 归约,并通过匿名类型投影出整洁的结果。

GroupBy、Join、Aggregate 与投影(匿名类型) 是 CoddyKit 上的免费 C# Academy 课时。 这是第 1 节课,共 3 节。 你可以在下方免费阅读本课时的完整内容 — 然后在浏览器中使用内置代码编辑器和全天候 AI 导师进行实践。 这是 C# Academy 学习路径的一部分,你的进度在网页和 CoddyKit 应用中同步。 C# Academy 课程共包含 3 节课。

高级运算符概览

本课内容:

  • GroupBy:按键划分桶
  • Join:根据键组合两个序列
  • Aggregate:归约为单个值
  • 匿名类型:让投影更简洁

按键分组

GroupBy 会创建分组,每个分组包含一个键和一个内部元素序列,其中的元素共享该键。

using System;
using System.Linq;

public class Program
{
  public static void Main(string[] args)
  {
    string[] words = new string[] { "ant", "cat", "tiger", "lion", "dog", "eagle" };

    var groups = words.GroupBy(w => w.Length);

    foreach (var g in groups)
    {
      Console.WriteLine("Len " + g.Key + ":");
      foreach (string w in g) Console.WriteLine(" - " + w);
    }
  }
}

按键进行内连接

Join 会根据键匹配两个序列中的元素,并投影组合后的结果。

using System;
using System.Linq;

public sealed class Customer { public int Id; public string Name; public Customer(int id, string name){ Id=id; Name=name; } }
public sealed class Order    { public int CustomerId; public string Item; public Order(int cid, string item){ CustomerId=cid; Item=item; } }

public class Program
{
  public static void Main(string[] args)
  {
    var customers = new Customer[] { new Customer(1,"Ada"), new Customer(2,"Bob") };
    var orders    = new Order[]    { new Order(1,"Book"), new Order(1,"Pen"), new Order(2,"Map") };

    var joined = customers.Join(
      orders,
      c => c.Id,
      o => o.CustomerId,
      (c, o) => c.Name + " -> " + o.Item);

    foreach (string s in joined) Console.WriteLine(s);
  }
}

Aggregate 基础

Aggregate 会对序列应用累加器,将其归约为一个结果。请选择合适的种子值。

using System;
using System.Linq;

public class Program
{
  public static void Main(string[] args)
  {
    int[] xs = new int[] { 1, 2, 3, 4 };

    // Product with seed 1
    int product = xs.Aggregate(1, (acc, n) => acc * n);
    Console.WriteLine("Product = " + product); // 24

    // Join words with a separator using seed
    string[] words = new string[] { "a", "b", "c" };
    string csv = words.Aggregate("", (acc, w) => acc == "" ? w : acc + "," + w);
    Console.WriteLine(csv); // a,b,c
  }
}

匿名类型投影

请使用匿名类型实现简洁的投影:new { Name = c.Name, ... },或使用简写形式 new { c.Name }。

using System;
using System.Linq;

public sealed class City { public string Name; public int Pop; public City(string n,int p){ Name=n; Pop=p; } }

public class Program
{
  public static void Main(string[] args)
  {
    var cities = new City[]
    {
      new City("Oslo", 634000),
      new City("Rome", 2873000),
      new City("Riga", 605000)
    };

    var small = cities
      .Where(c => c.Pop < 1000000)
      .Select(c => new { c.Name, Millions = (c.Pop / 1000000.0) });

    foreach (var x in small)
    {
      Console.WriteLine(x.Name + " ~ " + x.Millions + "M");
    }
  }
}

实用提示

提示:

  • 在 GroupBy 之后,先遍历各组,再遍历其中的元素。
  • 当您有两个键相匹配的源时,请使用 Join。
  • 为 Aggregate 选择有意义的种子值。
  • 匿名类型具有局部作用域;请从公共 API 返回具体的 DTO。

GroupBy 的用途

快速检查:哪个 LINQ 运算符会按键将元素分组,并返回分组序列?

总结

总结:使用 GroupBy 创建桶,使用 Join 组合源,使用 Aggregate 进行归约,并使用匿名类型实现简洁的投影。

常见问题解答

「GroupBy、Join、Aggregate 与投影(匿名类型)」课时是免费的吗?

是的 — 「GroupBy、Join、Aggregate 与投影(匿名类型)」的完整文本可在网页上免费阅读。要进行交互式练习(内置代码编辑器和全天候 AI 导师)并解锁 C# Academy 课程的其余内容,请升级到 CoddyKit PRO。 C# Academy 课程共包含 3 节课。

「GroupBy、Join、Aggregate 与投影(匿名类型)」这节课中我会学到什么?

对项目分组、连接序列、使用 Aggregate 归约,并通过匿名类型投影出整洁的结果。 你通过在浏览器中直接运行的动手代码来练习 C# Academy,全天候 AI 导师会在你学习这节课的过程中回答你的问题。

学习 C# Academy 需要有经验吗?

无需任何先前经验。CoddyKit 上的 C# Academy 课程适合初学者到高级学习者,你可以从这里开始或从头开始,按照自己的节奏学习。 这是第 1 节课,共 3 节。

「GroupBy、Join、Aggregate 与投影(匿名类型)」课时需要多长时间?

大多数 CoddyKit 课程大约需要 5–10 分钟。每节课都很精短且互动,所以你能稳步进步,并在网页和应用中从离开的地方继续。

我能在这节 C# Academy 课中编写并运行代码吗?

能。每节 C# Academy 课都包含内置代码编辑器,你可以在浏览器中直接编写并运行真实代码,并获得即时 AI 反馈 — 无需本地设置。

此课程中的所有课时

  1. GroupBy、Join、Aggregate 与投影(匿名类型)
  2. 具体化(ToList、ToDictionary)与性能提示
  3. 可组合性技巧
← 返回 C# Academy