C# Join

Join objects together

LINQ provides a fully featured query language. It offers the join operation. As with other query languages—such as SQL—joining matches every element in two collections based on some condition.

Example

Note

This example program uses two classes for storing data: the Customer class, which has an ID and a Name for each instance, and the Order class, which has an ID and a Product string for each instance. In the Main method, then, we use the abbreviated C# syntax for constructing the array instances; each has four elements. Finally, the query expression uses the join keyword to create new objects where the ID equals.

This C# example program uses the join keyword in a query expression. It requires System.Linq.

Program that uses join and equals in query [C#]

using System;
using System.Linq;

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

class Order
{
    public int ID { get; set; }
    public string Product { get; set; }
}

class Program
{
    static void Main()
    {
	// Example customers.
	var customers = new Customer[]
	{
	    new Customer{ID = 5, Name = "Sam"},
	    new Customer{ID = 6, Name = "Dave"},
	    new Customer{ID = 7, Name = "Julia"},
	    new Customer{ID = 8, Name = "Sue"}
	};

	// Example orders.
	var orders = new Order[]
	{
	    new Order{ID = 5, Product = "Book"},
	    new Order{ID = 6, Product = "Game"},
	    new Order{ID = 7, Product = "Computer"},
	    new Order{ID = 8, Product = "Shirt"}
	};

	// Join on the ID properties.
	var query = from c in customers
		    join o in orders on c.ID equals o.ID
		    select new { c.Name, o.Product };

	// Display joined groups.
	foreach (var group in query)
	{
	    Console.WriteLine("{0} bought {1}", group.Name, group.Product);
	}
    }
}

Output

Sam bought Book
Dave bought Game
Julia bought Computer
Sue bought Shirt
Question and answer

Query expression explanation. So how does the query expression work? Essentially, the customers array is used and each element from that array is identified as c. The orders array is also used, and each element from that is identified as o. The join takes the ID property from c and o and matches them. As the matches occur, new elements are created in the result.

Same as SQL. This syntax is basically the same as in SQL. Occasionally, it can be easier to develop logic in this style of language; unfortunately, using an imperative, low-level approach is likely faster in the C# language.

C# specification

The C# Programming Language

Part of this article is based on the C# language specification. In the specification, detailed examples are given for how query expressions are processed. This is near the end of the horrifying 143-page chapter on expressions (see page 333). Remember, thoroughness is a valuable attribute for language specifications.

The C# Programming Language: Specification

Summary

Using the LINQ extensions in the C# language, you can implement join expressions. This can make some matching routines more concise in your programs, and can help you convert SQL logic to C# code in some cases as well.

LINQ Examples
.NET