In this video Mohammad Azam will demonstrate how to use LINQ to SQL inner and outer left joins. He will also compare the LINQ to SQL approach with the traditional T-SQL approach.

Introduction:

In this video Mohammad Azam will demonstrate how to use LINQ to SQL inner and outer left joins. He will also compare the LINQ to SQL approach with the traditional T-SQL approach.

NorthwindDataContext db = new NorthwindDataContext();

            var query1 = (from c in db.Categories
                         join p in db.Products
                         on c.id equals p.CategoryID
                         select new { CategoryName = c.CategoryName }).Distinct();


            var query = (from c in db.Categories
                        join p in db.Products
                        on c.id equals p.CategoryID into joinedProducts

                        from joinedProduct in joinedProducts.DefaultIfEmpty()
                        select new { CategoryName = c.CategoryName }).Distinct();


            Console.WriteLine(query);

            foreach (var item in query)
            {
                Console.WriteLine(item.CategoryName);
            }

[Play the video]