In relational database terms, an inner join produces a result set in which each element of the first collection appears one time for every matching element in the second collection. If an element in the first collection has no matching elements, it does not appear in the result set. The Join method, which is called by the
join
clause in C#, implements an inner join.
Language Integrated Query (LINQ, pronounced “link”) is a Microsoft .NET Framework component that adds native data querying capabilities to .NET languages, released initially as a significant part of .NET Framework 3.5 in 2007.
In this post, I will show you how to generate inner join using LINQ to SQL. For this post, I am going to use the Categories and Product table of northwind.
Let’s imagine that you want to fetch CategoryName
andProductName
from the above table using LINQ.
var query=from p in Products
join c in Categories
on p.CategoryID equals c.CategoryID
select new
{
CategoryName=c.CategoryName,
ProductName=p.ProductName
};