In this video Mohammad Azam will demonstrate how to use Grouping using LINQ to SQL classes.

In this video Mohammad Azam will demonstrate how to use Grouping using LINQ to SQL classes.

static void Main(string[] args)
        {
            NorthwindDataContext northwind = new NorthwindDataContext();

            var list = from c in northwind.Categories
                       join p in northwind.Products
                       on c.id equals p.CategoryID
                       group p by c.CategoryName into products
                       select new
                       {
                           CategoryName = products.Key,
                           Products = products
                       };

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

                foreach (var product in item.Products)
                {
                    Console.WriteLine(product.ProductName);
                }

                Console.WriteLine("------------------");
            }


        }

[Play the video]