C# LINQ join Clause
The join clause of LINQ query expressions enables to associate the elements of multiple data source sequences. The join clause has the capability to compare the two sequences that have no direct relationship in their object models. It can compare the elements of different data sources if there is any property available in each sequence that can be compared for equality. For example, categories and products where you can create a list of products associated to different categories.
For joining two data source sequences you can use the join clause. The elements of one sequence must contain a property or value corresponding to one of the property of the other data source sequence. The join clause possesses the special "equals" keyword that enables to compare the equality between both the specified keys of the data sources.
Syntax for LINQ join Clause
from <1st range-variable> in <1st data source sequence object> join <2nd range-variable> in <2nd data source sequence object> on <1st range-variable>.<Key> equals <2nd range-variable>.<Key>
The above syntax for join clause shows that it involves two data sources. The from clause must be used to get the elements of first data source and the join clause can be used to get the elements of second data source. The contextual keyword "on" allows you to specify the comparison test between both the data sources using range-variable of first and second data sources along with their associated properties that can compared using "equals" keyword.
Example for LINQ join Clause using C# in ASP.Net
public class Category
{
public int CategoryID { get; set; }
public string CategoryName { get; set; }
public Category(int categoryID, string categoryName)
{
this.CategoryID = categoryID;
this.CategoryName = categoryName;
}
}
public class Product
{
public int ProductID { get; set; }
public string ProductName { get; set; }
public int CategoryID { get; set; }
public Product(int productID, string productName, int categoryID)
{
this.ProductID = productID;
this.ProductName = productName;
this.CategoryID = categoryID;
}
}
// first Data Source
List<Category> Categories = new List<Category>()
{
new Category(1, "Beverages"),
new Category(2, "Condiments"),
new Category(3, "Confections")
};
// second Data Source
List<Product> Products = new List<Product>()
{
new Product(1, "Chai", 1),
new Product(2, "Chang", 1),
new Product(3, "Aniseed Syrup", 2),
new Product(4, "Chef Anton's Gumbo Mix ", 2),
new Product(5, "Teatime Chocolate Biscuits", 3),
};
// LINQ join query to perform simple inner join
var query = from category in Categories
join product in Products on category.CategoryID equals product.CategoryID
select new { category.CategoryName, product.ProductName };
// loop over the query to get the result sequence
foreach (var item in query)
{
Response.Output.Write("{0}: {1}<br />", item.CategoryName, item.ProductName);
}
// Output:
// Beverages: Chai
// Beverages: Chang
// Condiments: Aniseed Syrup
// Condiments: Chef Anton's Gumbo Mix
// Confections: Teatime Chocolate Biscuits
In the above C# LINQ join clause example we have two data sources. The first data source has the list of categories and the second data source has the list of products. They can be compared based on the CategoryID property value that is available in both the data sources.
Continue to next tutorial: LINQ let Clause using C# in ASP.Net to learn how to declare a variable that can be used to store the enumerable results that can also be queried in sub expressions.

* will not be published
* hint: http://www.example.com