LINQ Technique: Group / Group Join

For this one, I'm trying to write a short series, aiming for 1 blog post every 1-2 weeks. I'll cover LINQ topics continuously until I run out of ideas. And this week about grouping

In LINQ, grouping is the operation of putting data into groups so that the elements in each group share a common attribute. you learn by the example below

Simple Grouping

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

public class Program
{
    public static void Main()
    {
        // Sample data
        List<string> words = new List<string> { "apple", "banana", "apricot", "blueberry", "cherry" };

        // Group words by their first letter
        var groupedWords = words.GroupBy(word => word[0]);

        // Display the results
        foreach (var group in groupedWords)
        {
            Console.WriteLine($"Key: {group.Key}");
            foreach (var word in group)
            {
                Console.WriteLine($"  {word}");
            }
        }
    }
}

Multiple Group-By

my example Group products By Category / Type

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

public class Program
{
    public static void Main()
    {
        // Sample data
        List<Product> products = new List<Product>
        {
            new Product { Category = "Fruit", Type = "Citrus", Name = "Orange" },
            new Product { Category = "Fruit", Type = "Berry", Name = "Strawberry" },
            new Product { Category = "Vegetable", Type = "Root", Name = "Carrot" },
            new Product { Category = "Vegetable", Type = "Leafy", Name = "Lettuce" },
            new Product { Category = "Fruit", Type = "Citrus", Name = "Lemon" }
        };

        // Group products by Category and then by Type
        var groupedProducts = products
            .GroupBy(p => p.Category)
            .Select(g => new
            {
                Category = g.Key,
                Types = g.GroupBy(p => p.Type)
            });

        // Display the results
        foreach (var categoryGroup in groupedProducts)
        {
            Console.WriteLine($"Category: {categoryGroup.Category}");
            foreach (var typeGroup in categoryGroup.Types)
            {
                Console.WriteLine($"  Type: {typeGroup.Key}");
                foreach (var product in typeGroup)
                {
                    Console.WriteLine($"    Product: {product.Name}");
                }
            }
        }
    }

    public class Product
    {
        public string Category { get; set; }
        public string Type { get; set; }
        public string Name { get; set; }
    }
}

Like SQL Join connects related data from two collections using a groupjoin, specifically, it connects customers with their orders.

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

public class Program
{
    public static void Main()
    {
        // Sample data
        List<Customer> customers = new List<Customer>
        {
            new Customer { CustomerId = 1, Name = "Alice" },
            new Customer { CustomerId = 2, Name = "Bob" }
        };

        List<Order> orders = new List<Order>
        {
            new Order { OrderId = 1, CustomerId = 1, Product = "Laptop" },
            new Order { OrderId = 2, CustomerId = 1, Product = "Mouse" },
            new Order { OrderId = 3, CustomerId = 2, Product = "Keyboard" }
        };

        // Group-Join: Connecting customers with their orders
        var customerOrders = customers.GroupJoin(
            orders,
            customer => customer.CustomerId,
            order => order.CustomerId,
            (customer, customerOrders) => new
            {
                CustomerName = customer.Name,
                Orders = customerOrders
            });

        // Display the results
        foreach (var customerOrder in customerOrders)
        {
            Console.WriteLine($"Customer: {customerOrder.CustomerName}");
            foreach (var order in customerOrder.Orders)
            {
                Console.WriteLine($"  Order: {order.Product}");
            }
        }
    }

    public class Customer
    {
        public int CustomerId { get; set; }
        public string Name { get; set; }
    }

    public class Order
    {
        public int OrderId { get; set; }
        public int CustomerId { get; set; }
        public string Product { get; set; }
    }
}


Discover more from naiwaen@DebuggingSoft

Subscribe to get the latest posts sent to your email.