C# Keywords Tutorial Part 50: join

C# Keywords Tutorial Part 50: join

C# is a popular choice for creating contemporary apps since it is a flexible programming language with many beneficial features. The “join” keyword is one such feature that enables programmers to integrate data from two or more collections based on a common key.

We will examine the C# “join” keyword and its potential applications in this blog article.

Based on a shared key, the “join” keyword is used to merge data from two or more collections. A new collection with the merged data is created by matching the elements in the collections using the key.

Here is an example of how to use the term “join”:

var orders = new List<Order>
{
    new Order { Id = 1, CustomerId = 1, OrderDate = new DateTime(2022, 1, 1), Total = 100.00 },
    new Order { Id = 2, CustomerId = 2, OrderDate = new DateTime(2022, 1, 2), Total = 200.00 },
    new Order { Id = 3, CustomerId = 1, OrderDate = new DateTime(2022, 1, 3), Total = 50.00 },
};

var customers = new List<Customer>
{
    new Customer { Id = 1, Name = "John Smith" },
    new Customer { Id = 2, Name = "Jane Doe" },
};

var ordersWithCustomers = from o in orders
                          join c in customers on o.CustomerId equals c.Id
                          select new
                          {
                              OrderId = o.Id,
                              CustomerName = c.Name,
                              OrderTotal = o.Total
                          };

In the given example, there are two collections: orders and customers. The orders collection has details regarding orders, including the customer id, while the customers collection has details about customers, including their id.

To combine the data from both collections based on the customer id, we utilize the “join” keyword. The outcome is a new collection comprising information about orders that include the corresponding customer name.

Moreover, the “join” keyword can also be used with multiple keys. Let’s take a look at an example below:

var orders = new List<Order>
{
    new Order { Id = 1, CustomerId = 1, OrderDate = new DateTime(2022, 1, 1), Total = 100.00 },
    new Order { Id = 2, CustomerId = 2, OrderDate = new DateTime(2022, 1, 2), Total = 200.00 },
    new Order { Id = 3, CustomerId = 1, OrderDate = new DateTime(2022, 1, 3), Total = 50.00 },
};

var customers = new List<Customer>
{
    new Customer { Id = 1, Name = "John Smith", State = "NY" },
    new Customer { Id = 2, Name = "Jane Doe", State = "CA" },
};

var ordersWithCustomers = from o in orders
                          join c in customers on new { o.CustomerId, o.OrderDate.Year } equals new { c.Id, Year = 2022 }
                          select new
                          {
                              OrderId = o.Id,
                              CustomerName = c.Name,
                              CustomerState = c.State,
                              OrderTotal = o.Total
                          };

In this example, we use the “join” keyword with two keys: the customer id and the order date year. We use anonymous types to create a composite key for each collection, and we join the collections on these keys.

Share this post

Leave a Reply

Your email address will not be published. Required fields are marked *