Linq Group Join in C#

Introduction: Linq Group Join in C# is a powerful feature that allows developers to group elements from two collections based on a common key. This is useful when working with relational data, where you need to group data from multiple tables based on a common field. In this lesson, we’ll explore how to perform a Linq group join in C# and provide examples and usage tips to help you master this feature.

Syntax: The syntax for a Linq group join in C# is as follows:
var query =
   from collection1 in collection1List
   join collection2 in collection2List
   on collection1.Key equals collection2.Key into joinedCollection
   select new
   {
       Key = collection1.Key,
       Collection1Items = collection1.Items,
       Collection2Items = joinedCollection.Select(c2 => c2.Items)
   };
Explanation:
  • The first line declares a variable to hold the result of the query.
  • The second line specifies the first collection to join on.
  • The third line specifies the second collection to join on.
  • The fourth line specifies the common key to join on.
  • The fifth line uses the “into” keyword to create a temporary variable to hold the joined collection.
  • The sixth line creates a new object to hold the result of the query. This object contains the common key, the items from the first collection, and the items from the joined collection.
Example: Let’s assume we have two collections, “orders” and “customers”, and we want to group the orders by customer:
class Order
{
   public int OrderID { get; set; }
   public string OrderName { get; set; }
   public int CustomerID { get; set; }
}

class Customer
{
   public int CustomerID { get; set; }
   public string CustomerName { get; set; }
}

List<Customer> customers = new List<Customer>()
{
   new Customer { CustomerID = 1, CustomerName = "John Doe" },
   new Customer { CustomerID = 2, CustomerName = "Jane Doe" },
   new Customer { CustomerID = 3, CustomerName = "Bob Smith" }
};

List<Order> orders = new List<Order>()
{
   new Order { OrderID = 1, OrderName = "Order 1", CustomerID = 1 },
   new Order { OrderID = 2, OrderName = "Order 2", CustomerID = 1 },
   new Order { OrderID = 3, OrderName = "Order 3", CustomerID = 2 },
   new Order { OrderID = 4, OrderName = "Order 4", CustomerID = 3 },
   new Order { OrderID = 5, OrderName = "Order 5", CustomerID = 3 }
};

var query =
   from customer in customers
   join order in orders
   on customer.CustomerID equals order.CustomerID into customerOrders
   select new
   {
       CustomerName = customer.CustomerName,
       Orders = customerOrders.Select(o => o.OrderName)
   };

foreach (var customerOrder in query)
{
   Console.WriteLine(customerOrder.CustomerName);
   foreach (var order in customerOrder.Orders)
   {
       Console.WriteLine("    " + order);
   }
}

Output:
John Doe
Order 1
Order 2
Jane Doe
Order 3
Bob Smith
Order 4
Order 5
Explanation:
  • We first declare two collections, “customers” and “orders”.
  • We then create a Linq query to group the orders by customer.
  • In the query, we use a join clause to join the “customers” and “
  • In the query, we use a join clause to join the “customers” and “orders” collections on the “CustomerID” field. The “into” keyword is used to group the orders by customer.
  • We then create a new object to hold the result of the query. This object contains the customer name and the orders associated with that customer.
  • Finally, we iterate over the result of the query and print out the customer name and associated orders.

Usage tips:

  • Linq group join is useful when working with relational data, where you need to group data from multiple tables based on a common field.
  • You can use Linq group join with any two collections that have a common field to group by.
  • When using Linq group join, it’s important to ensure that the join fields are of the same data type, otherwise the join will not work as expected.
  • If you need to group the results of the Linq group join by more than one field, you can simply add additional fields to the “new” object created in the select clause.
  • You can also use Linq group join with lambda expressions and method syntax, as well as with other Linq operators such as where, select, and orderby.
Conclusion:

Linq group join in C# is a powerful feature that allows developers to group elements from two collections based on a common key. This is useful when working with relational data, where you need to group data from multiple tables based on a common field. By using the Linq group join syntax, you can create complex queries that allow you to manipulate and analyze data in a variety of ways. With the usage tips and examples provided in this lesson, you should be well on your way to mastering the Linq group join feature in C#.