EF Join three tables

By   Tewodros   Date Posted: May. 28, 2024  Hits: 254   Category:  Database   Total Comment: 0             A+ A-


side

using System;

using System.Collections.Generic;

using System.Linq;

 

public class Product

{

   public int ProductId { get; set; }

   public string Name { get; set; }

   public decimal Price { get; set; }

   public int CategoryId { get; set; }

}

 

public class Category

{

   public int CategoryId { get; set; }

   public string Name { get; set; }

}

 

public class Order

{

   public int OrderId { get; set; }

   public int ProductId { get; set; }

   public int Quantity { get; set; }

}

 

 

public class Program

{

   public static void Main()

   {

       List<Product> productList = new List<Product>

       {

           new Product { ProductId = 1, Name = "Product1", Price = 100, CategoryId = 1 },

           new Product { ProductId = 2, Name = "Product2", Price = 200, CategoryId = 1 },

           new Product { ProductId = 3, Name = "Product3", Price = 300, CategoryId = 2 }

       };

 

       List<Category> categoryList = new List<Category>

       {

           new Category { CategoryId = 1, Name = "Category1" },

           new Category { CategoryId = 2, Name = "Category2" }

       };

       List<Order> OrderList = new List<Order>

       {

           new Order { OrderId = 1, ProductId = 1, Quantity = 5 },

           new Order { OrderId = 2, ProductId = 1, Quantity = 3 },

           new Order { OrderId = 3, ProductId = 2, Quantity = 2 },

           new Order { OrderId = 4, ProductId = 3, Quantity = 4 },

           new Order { OrderId = 5, ProductId = 3, Quantity = 1 }

       };

 

       var results = from product in productList                      

                    join category in categoryList on product.CategoryId equals category.CategoryId

                    select new

                    {

                        ProductId = product.ProductId,

                        ProductName = product.Name,

                        CategoryName = category.Name,

                       

                    };

 

 

       var result = from product in productList

                    join order in OrderList on product.ProductId equals order.ProductId

                    join category in categoryList on product.CategoryId equals category.CategoryId

                    select new

                    {

                        ProductId = product.ProductId,

                        ProductName = product.Name,

                        CategoryName = category.Name,

                        OrderQuantity = order.Quantity

                    };

 

       foreach (var item in result)

       {

           Console.WriteLine($"Category: {item.CategoryName}, Product: {item.ProductName}, Order Quantity: {item.OrderQuantity}");

       }

 

 

   }

}

 


Tags



Back to Top



Related Blogs






Please fill all fields that are required and click Add Comment button.

Name:*
Email:*
Comment:*
(Only 2000 char allowed)


Security Code:* tlcvxo

Back to Top