using System.Collections.Generic;
public static void Main()
foreach (var item in GetAll())
Console.WriteLine("CategoryName: {0}", item.Key);
foreach(var product in item)
Console.WriteLine("Product: |_ {0} - {1}", product.ProductName,product.ColorName);
public static IQueryable<IGrouping<string,GroupViewModel>> GetAll()
List<Category> listCategories = new List<Category>
Id = 1, CateName = "SmartPhone"
Id = 2, CateName = "Laptop"
Id = 3, CateName = "Desktop"
List<Product> listProducts = new List<Product>
Id = 1, ProdName = "Lumia 720", CategoryId = 1, ColorId = 2
Id = 2, ProdName = "PC HP", CategoryId = 3, ColorId = 1
Id = 3, ProdName = "PC Dell", CategoryId = 3, ColorId = 1
Id = 4, ProdName = "Laptop Lenovo", CategoryId = 2, ColorId = 2
Id = 5, ProdName = "Lumia 920", CategoryId = 1, ColorId = 2
Id = 6, ProdName = "Laptop Dell", CategoryId = 2, ColorId = 3
Id = 7, ProdName = "Laptop HP", CategoryId = 2, ColorId = 3
List<Color> listColor = new List<Color>
ColorId = 1, ColorName = "Blue"
ColorId = 2, ColorName = "Yellow"
ColorId = 3, ColorName = "Red"
join cl in listColor on p.ColorId equals cl.ColorId
join c in listCategories on p.CategoryId equals c.Id into e
from j in e.DefaultIfEmpty()select new GroupViewModel
ProductId = p.Id, ProductName = p.ProdName, CategoryId = j.Id, CateogryName = j.CateName, ColorId = cl.ColorId, ColorName = cl.ColorName
).GroupBy(x => x.CateogryName);
return query.AsQueryable<IGrouping<string,GroupViewModel>>();
public class GroupViewModel
public string ProductName
public string CateogryName
public string CateogryName
public List<Product> Products