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!

 

 public class Brand
    {
        public int Id { get; set; }
        public string Name { get; set; }
    }

    public class Category
    {
        public int Id { get; set; }
        public string Name { get; set; }
    }

    public class Product
    {
        public int Id { get; set; }
        public int CategoryId { get; set; }
        public string Name { get; set; }
        public Brand Brand { get; set; }
    }

 

and some fake data:

 

  public class Data
    {
        static List<Brand> _brands = new List<Brand>()
            {
                new Brand() {Id = 1, Name = "Best"},
            };


        static List<Category> _categories = new List<Category>()
            {
                new Category() {Id = 1, Name = "Sextual"},
                new Category() {Id = 2, Name = "Sport"}
            };

        static List<Product> _products = new List<Product>()
            {
                new Product() {Id = 1, Name = "Candom!",CategoryId = 1, Brand=_brands.Single(b=>b.Name=="Best")},
                new Product() {Id = 2, Name = "Dildo!",CategoryId =1, Brand=_brands.Single(b=>b.Name=="Best") }
            };

 

the queries:

 

  public class InnerJoin
        {

            void Sample1()
            {
                var data = from category in _categories
                           join product in _products on category.Id equals product.CategoryId
                           select new { Category = category.Id, Product = product.Name };
            }

            void Sample2()
            {
                var data = from category in _categories
                           join product in _products on new {p = category.Name} equals new {p = product.Name = "Mature"}
                           select new
                               {
                                   category.Id,
                                   category.Name
                               };
            }
            void Sample3()
            {
                var data = from brand in _brands
                           from product in _products.Where(b => b.Brand.Name == "Best" || b.CategoryId == b.Id)
                           select new
                               {
                                   brand.Name,
                                   brand.Id
                               };
            }
          
        }

        public class LeftJoin
        {
            void Sample1()
            {
                var data = from brand in _brands
                           join product in _products on brand.Id equals product.Brand.Id into productGroup
                           from g in productGroup.DefaultIfEmpty()
                           select new
                               {
                                   brand.Id,
                                   brand.Name,
                               };
            }
        }

        public class GroupJoin
        {
            void Sample1()
            {
                var data = from category in _categories
                           join product in _products on category.Id equals product.CategoryId into productGroup
                           select productGroup;
            }
        }

        public class GroupInnerJoin
        {
            void Sampel()
            {
                var data = from category in _categories
                           join product in _products on category.Id equals product.CategoryId into productGroup
                           select new
                               {
                                   Category = category.Name,
                                   Product = from product in productGroup
                                             select product
                               };
            }
        }

        public class OuterJoin
        {
           void Sample1()
           {
               var data = from category in _categories
                          join product in _products
                              on category.Id equals product.CategoryId
                              into productGroup
                          from productCategory in productGroup.DefaultIfEmpty()
                          select new
                              {
                                  Category = category.Name,
                                  category = productCategory.Name
                              };

           }
        }


        public class LeftOuterJoin
        {
            void Sample1()
            {
                var data = from category in _categories
                           join product in _products on category.Id equals product.CategoryId into productGroup
                           select productGroup.DefaultIfEmpty(new Product() { Id = 3, Name = "Style!", CategoryId = 3 });


            }
        }
    }

 

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 and I'm spending a lot of my time on architecture subject. Since 2008, I've been as a developer for companies and organizations and I've been focusing on Microsoft ecosystem all the time. During the&nb Read More

Post Tags
Pending Blog Posts
Strategic design
Factory Pattern
time out pattern in ajax
Selectors in Jquery
Peridic pattern
How to use PagedList In asp.net MVC
Redis as a cache server
Domain driven design VS model driven architecture
How to query over Icollection<> of a type with linq
What's the DDD-lite?
comments powered by Disqus