Ehsan Ghanbari

Experience, DotNet, Solutions

Practicing different kinds of joins via Linq

I'm interested in to share these samples about different kinds of joins via LINQ. have fun!

  1.     public class Brand
  2.     {
  3.         public int Id { get; set; }
  4.         public string Name { get; set; }
  5.     }
  6.  
  7.     public class Category
  8.     {
  9.         public int Id { get; set; }
  10.         public string Name { get; set; }
  11.     }
  12.  
  13.     public class Product
  14.     {
  15.         public int Id { get; set; }
  16.         public int CategoryId { get; set; }
  17.         public string Name { get; set; }
  18.         public Brand Brand { get; set; }
  19.     }

 

 

  1.     public class Data
  2.     {
  3.         static List<Brand> _brands = new List<Brand>()
  4.             {
  5.                 new Brand() {Id = 1, Name = "Best"},
  6.             };
  7.  
  8.  
  9.         static List<Category> _categories = new List<Category>()
  10.             {
  11.                 new Category() {Id = 1, Name = "Sextual"},
  12.                 new Category() {Id = 2, Name = "Sport"}
  13.             };
  14.  
  15.         static List<Product> _products = new List<Product>()
  16.             {
  17.                 new Product() {Id = 1, Name = "Candom!",CategoryId = 1, Brand=_brands.Single(b=>b.Name=="Best")},
  18.                 new Product() {Id = 2, Name = "Dildo!",CategoryId =1, Brand=_brands.Single(b=>b.Name=="Best") }
  19.             };

 

 

 

 

  1.         public class InnerJoin
  2.         {
  3.  
  4.             void Sample1()
  5.             {
  6.                 var data = from category in _categories
  7.                            join product in _products on category.Id equals product.CategoryId
  8.                            select new { Category = category.Id, Product = product.Name };
  9.             }
  10.  
  11.             void Sample2()
  12.             {
  13.                 var data = from category in _categories
  14.                            join product in _products on new {p = category.Name} equals new {p = product.Name = "Mature"}
  15.                            select new
  16.                                {
  17.                                    category.Id,
  18.                                    category.Name
  19.                                };
  20.             }
  21.             void Sample3()
  22.             {
  23.                 var data = from brand in _brands
  24.                            from product in _products.Where(b => b.Brand.Name == "Best" || b.CategoryId == b.Id)
  25.                            select new
  26.                                {
  27.                                    brand.Name,
  28.                                    brand.Id
  29.                                };
  30.             }
  31.           
  32.         }
  33.  
  34.         public class LeftJoin
  35.         {
  36.             void Sample1()
  37.             {
  38.                 var data = from brand in _brands
  39.                            join product in _products on brand.Id equals product.Brand.Id into productGroup
  40.                            from g in productGroup.DefaultIfEmpty()
  41.                            select new
  42.                                {
  43.                                    brand.Id,
  44.                                    brand.Name,
  45.                                };
  46.             }
  47.         }
  48.  
  49.         public class GroupJoin
  50.         {
  51.             void Sample1()
  52.             {
  53.                 var data = from category in _categories
  54.                            join product in _products on category.Id equals product.CategoryId into productGroup
  55.                            select productGroup;
  56.             }
  57.         }
  58.  
  59.         public class GroupInnerJoin
  60.         {
  61.             void Sampel()
  62.             {
  63.                 var data = from category in _categories
  64.                            join product in _products on category.Id equals product.CategoryId into productGroup
  65.                            select new
  66.                                {
  67.                                    Category = category.Name,
  68.                                    Product = from product in productGroup
  69.                                              select product
  70.                                };
  71.             }
  72.         }
  73.  
  74.         public class OuterJoin
  75.         {
  76.            void Sample1()
  77.            {
  78.                var data = from category in _categories
  79.                           join product in _products
  80.                               on category.Id equals product.CategoryId
  81.                               into productGroup
  82.                           from productCategory in productGroup.DefaultIfEmpty()
  83.                           select new
  84.                               {
  85.                                   Category = category.Name,
  86.                                   category = productCategory.Name
  87.                               };
  88.  
  89.            }
  90.         }
  91.  
  92.  
  93.         public class LeftOuterJoin
  94.         {
  95.             void Sample1()
  96.             {
  97.                 var data = from category in _categories
  98.                            join product in _products on category.Id equals product.CategoryId into productGroup
  99.                            select productGroup.DefaultIfEmpty(new Product() { Id = 3, Name = "Style!", CategoryId = 3 });
  100.  
  101.  
  102.             }
  103.         }
  104.     }

 

 

Cheers!

About Me

Ehsan Ghanbari

Hi! my name is Ehsan. I'm a developer, passionate technologist, and fan of clean code. I'm interested in enterprise and large-scale applications architecture and design patterns. I spend a lot of time on software architecture. Since 2008, I've been as a developer for different companies and organizations and I've been focusing on Microsoft ecosystem all the time. During the past years, Read More

Post Tags
Pending Blog Posts
using Elmah in asp.net MVC4
Using FluentSecurity in MVC
Strategic design
Factory Pattern
time out pattern in ajax
Redis as a cache server
How to use PagedList In asp.net MVC
Multiple submit buttons in asp.net MVC
Domain driven design VS model driven architecture
What's the DDD-lite?
comments powered by Disqus