Introduction

In this article, I will demonstrate how we can perform join operation with multiple SQL database tables using Language-Integrated Query (LINQ).

Step 1

Open SQL Server 2014 and create a database table

Table 1 - Customer

  1. CREATE TABLE [dbo].[Customer](
  2. [CustId] [int] IDENTITY(1000,1) NOT NULL,
  3. [CustomerName] [nvarchar](50) NULL,
  4. [PhoneNumber] [nvarchar](50) NULL,
  5. [Email] [nvarchar](50) NULL,
  6. [ShippingAddress] [nvarchar](max) NULL,
  7. [City] [nvarchar](50) NULL,
  8. [State] [nvarchar](50) NULL,
  9. [Country] [nvarchar](50) NULL,
  10. [PostalCode] [nvarchar](50) NULL,
  11. CONSTRAINT [PK_Customer] PRIMARY KEY CLUSTERED
  12. (
  13. [CustId] ASC
  14. )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
  15. ) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]
  16. GO

Table 2 - Product

  1. CREATE TABLE [dbo].[Product](
  2. [ProductId] [int] IDENTITY(100,1) NOT NULL,
  3. [ProductName] [nvarchar](50) NULL,
  4. [ProductDescription] [nvarchar](max) NULL,
  5. [ProductPrice] [money] NULL,
  6. [ProductCategoryId] [int] NULL,
  7. CONSTRAINT [PK_Product] PRIMARY KEY CLUSTERED
  8. (
  9. [ProductId] ASC
  10. )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
  11. ) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]
  12. GO
  13. ALTER TABLE [dbo].[Product] WITH CHECK ADD CONSTRAINT [FK_Product_Category1] FOREIGN KEY([ProductCategoryId])
  14. REFERENCES [dbo].[Category] ([CategoryId])
  15. GO
  16. ALTER TABLE [dbo].[Product] CHECK CONSTRAINT [FK_Product_Category1]
  17. GO

Table 3 - Category

  1. CREATE TABLE [dbo].[Category](
  2. [CategoryId] [int] IDENTITY(1,1) NOT NULL,
  3. [CategoryName] [nvarchar](50) NULL,
  4. [CategoryDecription] [nvarchar](50) NULL,
  5. CONSTRAINT [PK_Category] PRIMARY KEY CLUSTERED
  6. (
  7. [CategoryId] ASC
  8. )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
  9. ) ON [PRIMARY]
  10. GO

Table 4 - CustomerOrder

  1. CREATE TABLE [dbo].[CustomerOrder](
  2. [OrderId] [int] IDENTITY(1,1) NOT NULL,
  3. [CustomerId] [int] NULL,
  4. [ProductId] [int] NULL,
  5. [CategoryId] [int] NULL,
  6. [Quantity] [int] NULL,
  7. [OrderDate] [date] NULL,
  8. CONSTRAINT [PK_Order] PRIMARY KEY CLUSTERED
  9. (
  10. [OrderId] ASC
  11. )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
  12. ) ON [PRIMARY]
  13. GO
  14. ALTER TABLE [dbo].[CustomerOrder] WITH CHECK ADD CONSTRAINT [FK_CustomerOrder_Category] FOREIGN KEY([CategoryId])
  15. REFERENCES [dbo].[Category] ([CategoryId])
  16. GO
  17. ALTER TABLE [dbo].[CustomerOrder] CHECK CONSTRAINT [FK_CustomerOrder_Category]
  18. GO
  19. ALTER TABLE [dbo].[CustomerOrder] WITH CHECK ADD CONSTRAINT [FK_CustomerOrder_Customer] FOREIGN KEY([CustomerId])
  20. REFERENCES [dbo].[Customer] ([CustId])
  21. GO
  22. ALTER TABLE [dbo].[CustomerOrder] CHECK CONSTRAINT [FK_CustomerOrder_Customer]
  23. GO
  24. ALTER TABLE [dbo].[CustomerOrder] WITH CHECK ADD CONSTRAINT [FK_CustomerOrder_Product] FOREIGN KEY([ProductId])
  25. REFERENCES [dbo].[Product] ([ProductId])
  26. GO
  27. ALTER TABLE [dbo].[CustomerOrder] CHECK CONSTRAINT [FK_CustomerOrder_Product]
  28. GO

Step 2

Open Visual Studio 2015 and create a new console application with a meaningful name.

Step 3

Add Entity Framework now. For that, right click on Models folder, select Add, then select New Item, then click on it.

Screenshot for adding Entity Framework 1

How to use Join operation with multiple SQL database tables Using LINQ

After clicking on the new item, you will get a window; from there, select Data from the left panel and choose ADO.NET Entity Data Model, give it the name DBModels (this name is not mandatory you can give any name) and click on Add.

Screenshot for adding Entity Framework 2

How to use Join operation with multiple SQL database tables Using LINQ

After you click on "Add a window", the wizard will open, choose EF Designer from a database and click next.

Screenshot for adding Entity Framework 3

How to use Join operation with multiple SQL database tables Using LINQ

After clicking on Next a window will appear. Choose New Connection. Another window will appear, add your server name if it is local then enter a dot (.). Choose your database and click on OK.

Screenshot for adding Entity Framework 4

How to use Join operation with multiple SQL database tables Using LINQ

The connection will be added. If you wish to save, connect as you want. You can change the name of your connection below. It will save connection in web config then click on Next.

Screenshot for adding Entity Framework 5

How to use Join operation with multiple SQL database tables Using LINQ

After clicking on NEXT another window will appear -- choose database table name as shown in the below screenshot then click on Finish.

Screenshot for adding Entity Framework 6

How to use Join operation with multiple SQL database tables Using LINQ

Entity Framework will be added and respective class gets generated under the Models folder.

Screenshot for adding Entity Framework 7

How to use Join operation with multiple SQL database tables Using LINQ

Example for Multiple Tables Join

  1. using System;
  2. using System.Linq;
  3. namespace MultiTableJoin_Demo
  4. {
  5. class Program
  6. {
  7. static void Main(string[] args)
  8. {
  9. using (DBModel db=new DBModel())
  10. {
  11. var results = from o in db.CustomerOrders
  12. join c in db.Customers on o.CustomerId equals c.CustId
  13. join p in db.Products on o.ProductId equals p.ProductId
  14. join cate in db.Categories on o.CategoryId equals cate.CategoryId
  15. select new {
  16. o.OrderId,
  17. c.CustomerName,
  18. c.PhoneNumber,
  19. c.ShippingAddress,
  20. c.PostalCode,
  21. p.ProductName,
  22. p.ProductPrice,
  23. o.Quantity,
  24. o.OrderDate
  25. };
  26. foreach (var customerOrder in results)
  27. {
  28. Console.WriteLine(" Order ID:\t\t" + customerOrder.OrderId + "\n");
  29. Console.WriteLine(" Customer Name:\t\t" + customerOrder.CustomerName + "\n");
  30. Console.WriteLine(" Phone Number:\t\t" + customerOrder.PhoneNumber + "\n");
  31. Console.WriteLine(" Shipping Address:\t" + customerOrder.ShippingAddress + "\n");
  32. Console.WriteLine(" Postal Code:\t\t" + customerOrder.PostalCode + "\n");
  33. Console.WriteLine(" Product Name:\t\t" + customerOrder.ProductName + "\n");
  34. Console.WriteLine(" Product Price:\t\t" + customerOrder.ProductPrice + "\n");
  35. Console.WriteLine(" Quantity:\t\t" + customerOrder.Quantity + "\n");
  36. Console.WriteLine(" Total Amount:\t\t"+customerOrder.ProductPrice*customerOrder.Quantity + "\n");
  37. Console.WriteLine(" Order Date:\t\t" + customerOrder.OrderDate + "\n");
  38. Console.WriteLine("-----------------------------------------------------");
  39. }
  40. Console.ReadLine();
  41. }
  42. }
  43. }
  44. }

Output

How to use Join operation with multiple SQL database tables Using LINQ