0Pricing
C# Academy · Lesson

GroupBy, Join, Aggregate, projections (anonymous types)

Group items, join sequences, reduce with Aggregate, and project clean results using anonymous types.

Advanced operators overview

Today:

  • GroupBy: make buckets by key
  • Join: combine two sequences on a key
  • Aggregate: reduce to a single value
  • Anonymous types: tidy projections

Grouping by key

GroupBy creates groups with a Key and an inner sequence of items sharing that key.

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);
    }
  }
}

All lessons in this course

  1. GroupBy, Join, Aggregate, projections (anonymous types)
  2. Materialization (ToList, ToDictionary) & perf notes
  3. Composability tips
← Back to C# Academy