Linq Join

Cross Join
This sample shows how to efficiently join elements of two sequences based on equality between key expressions over the two.

class LinqJoin
    {
        static void Main(string[] args)
        {

            List<Customer> customer = new List<Customer>()
            {
                new Customer{ ID=1,Name="Bhaumik",Status=true},
                new Customer{ ID=2,Name="Hardik",Status=true},
                new Customer{ ID=3,Name="Rachit",Status=false},
                new Customer{ ID=4,Name="Darshan",Status=true},
                new Customer{ ID=5,Name="Manan",Status=false},
                new Customer{ ID=6,Name="Malay",Status=true},
                new Customer{ ID=7,Name="Vishal",Status=false},
                new Customer{ ID=8,Name="Raj",Status=true},
                new Customer{ ID=9,Name="Rohit",Status=false}
            };

            string[] Name = new string[] { "Bhaumik", "Rachit", "Manan", "Vishal"};

            var q = from c in Name
                    join p in customer on c equals p.Name
                    select new { Name = c, p.ID };

            foreach (var v in q)
            {
                Console.WriteLine(v.ID + ": " + v.Name);
            }
            Console.ReadKey(true);
       } 
}
Output


Group Join
Using a group join you can get all the products that match a given category bundled as a sequence.
 string[] categories = new string[]{ 
        "Beverages",
        "Condiments",
        "Vegetables",
        "Dairy Products",
        "Seafood" };

    List<Product> products = GetProductList();
    var q =
        from c in categories
        join p in products on c equals p.Category into ps
        select new { Category = c, Products = ps };

    foreach (var v in q)
    {
        Console.WriteLine(v.Category + ":");

        foreach (var p in v.Products)
        {
            Console.WriteLine("   " + p.ProductName);
        }
    }
Cross Join with Group Join
The group join operator is more general than join, as this slightly more verbose version of the cross join sample shows.
string[] categories = new string[]{ 
        "Beverages", 
        "Condiments", 
        "Vegetables", 
        "Dairy Products", 
        "Seafood" };

    List<Product> products = GetProductList();

    var q =
        from c in categories
        join p in products on c equals p.Category into ps
        from p in ps
        select new { Category = c, p.ProductName };

    foreach (var v in q)
    {
        Console.WriteLine(v.ProductName + ": " + v.Category);
    }
Left Outer Join
A so-called outer join can be expressed with a group join. A left outer joinis like a cross join, except that all the left hand side elements get included at least once, even if they don't match any right hand side elements. Note how Vegetablesshows up in the output even though it has no matching products.

string[] categories = new string[]{ 
        "Beverages", 
        "Condiments", 
        "Vegetables", 
        "Dairy Products", 
        "Seafood" };

    List<Product> products = GetProductList();

    var q =
        from c in categories
        join p in products on c equals p.Category into ps
        from p in ps.DefaultIfEmpty()
        select new { Category = c, ProductName = p == null ? "(No products)" : p.ProductName };
  
    foreach (var v in q)
    {
        Console.WriteLine(v.ProductName + ": " + v.Category);
    }
Linq Join Linq Join Reviewed by Unknown on 8:32 PM Rating: 5