0Pricing
C# Academy · Lesson

GroupBy, Join, Aggregate, projections (anonymous types)

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

GroupBy, Join, Aggregate, projections (anonymous types) is a free C# Academy lesson on CoddyKit — lesson 1 of 3. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the C# Academy learning path, one of 3 lessons in the course, and your progress syncs across the web and the CoddyKit app.

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

Inner join by key

Join matches items from two sequences by key and projects the combined result.

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 basics

Aggregate applies an accumulator to reduce a sequence to one result. Pick a good seed.

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

Anonymous type projection

Use anonymous types for neat projections: new { Name = c.Name, ... } or shorthand 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");
    }
  }
}

Practical tips

Tips:

  • After GroupBy, iterate groups then items.
  • Use Join when you have two sources with matching keys.
  • Pick a seed that makes sense for Aggregate.
  • Anonymous types are local; return concrete DTOs from public APIs.

GroupBy purpose

Quick check: Which LINQ operator groups elements by a key and returns a sequence of groups?

Recap

Recap: Use GroupBy for buckets, Join to combine sources, Aggregate to reduce, and anonymous types for clean projections.

Frequently asked questions

Is the “GroupBy, Join, Aggregate, projections (anonymous types)” lesson free?

Yes — the full text of “GroupBy, Join, Aggregate, projections (anonymous types)” is free to read here on the web, and the C# Academy course includes 3 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the C# Academy course, upgrade to CoddyKit PRO.

What will I learn in “GroupBy, Join, Aggregate, projections (anonymous types)”?

Group items, join sequences, reduce with Aggregate, and project clean results using anonymous types. You practise C# Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start C# Academy?

No prior experience is required. C# Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 1 of 3, so you can start here or from the beginning and move at your own pace.

How long does the “GroupBy, Join, Aggregate, projections (anonymous types)” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this C# Academy lesson?

Yes. Every C# Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

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