join operator in linq

Similar to the join operator in SQL, LINQ also provides a join operator. The Join operator joins the objects between different data sources based on a common attribute. For example we can join two collections or List and dictionary or database table and list etc. The join operator performs the inner join operation. The example given demonstrates how to query two different data sources, one being List and another dictionary. The List contains category of products and the dictionary contains products.

List categories = new List(); 
categories.Add("Vegetables");     
categories.Add("Fruits");                   
categories.Add("Non Sodium Items");                                
categories.Add("Bakery Products");
Dictionary products;
products = new Dictionary();
products.Add("Product1","Vegetables");
products.Add("Product2","Vegetables");
products.Add("Product3","Non Sodium Items" );
products.Add("Product4","Non Sodium Items" );
products.Add("Product5","Bakery Products" );
var q =
       from c in categories
       join p in products on c equals p.Value
       select new { Category = c, Product =p.Key };
       foreach (var val in q)
       {
          Console.WriteLine("{0} {1}", val.Product, val.Category);
       }


The above query joins the List collection and the Dictionary. The join operator condition is equals. The equals keyword means when a category string in List matches with the value argument of the dictionary then that data is selected for output.

When a join clause is used, LINQ checks every element in the collection and dictionary and compares which ones satisfy the condition. The select projection operator creates an output with category from the collection and product name from the dictionary.