C# Program to Join Multiple Data Sources Using LINQ Join() Method

What we are going to look at through this article is C# Program to Join Multiple Data Sources Using LINQ Join() Method We will look at this in detail through this article.

C# Program to Join Multiple Data Sources Using LINQ Join() Method

C# Program to Join Multiple Data Sources Using LINQ Join() Method

//C# program to join multiple data sources 
//using Linq Join() Method.

using System;
using System.Linq;
using System.Collections.Generic;

public class Employee
{
    public int ID;
    public int Salary;
    public int DEPT_ID;
    public int ADD_ID;
    public string Name;
}

public class Department
{
    public int DEPT_ID;
    public string DEPT_Name;
}

public class Address
{
    public int ADD_ID;
    public string address;
}

public class JoinDemo
{
    static void Main(string[] args)
    {

        List<Employee> employees = new List<Employee>()
        {
             new Employee {ID=101,   Name="Amit  "    , Salary=4000,DEPT_ID=101,ADD_ID=201},
             new Employee {ID=102,   Name="Amit  "    , Salary=3800,DEPT_ID=102,ADD_ID=202},
             new Employee {ID=103,   Name="Salman"    , Salary=3500,DEPT_ID=103,ADD_ID=203},
             new Employee {ID=104,   Name="Ram   "    , Salary=2000,DEPT_ID=101,ADD_ID=204},
             new Employee {ID=105,   Name="Shyam "    , Salary=7000,DEPT_ID=102,ADD_ID=205},
             new Employee {ID=106,   Name="Kishor"    , Salary=5000,DEPT_ID=103,ADD_ID=206},
        };

        List<Department> departments = new List<Department>()
        {
             new Department {DEPT_ID=101,   DEPT_Name="HR        "   },
             new Department {DEPT_ID=102,   DEPT_Name="ACCOUNTS  "   },
             new Department {DEPT_ID=103,   DEPT_Name="SALES     "   },
        };

        List<Address> addresses = new List<Address>()
        {
             new Address {ADD_ID=201,   address="AGRA      "   },
             new Address {ADD_ID=202,   address="BAMBAI    "   },
             new Address {ADD_ID=203,   address="CHENNAI   "   },
             new Address {ADD_ID=204,   address="DELHI     "   },
             new Address {ADD_ID=205,   address="GWALIOR   "   },
             new Address {ADD_ID=206,   address="JHASHI    "   },
        };


        var Result = employees.Join(departments,
                                    emp1 => emp1.DEPT_ID,
                                    dept => dept.DEPT_ID,
                                    (emp1, dept)=>new {emp1, dept}
                                    )
                              .Join(addresses,
                                    emp2 => emp2.emp1.ADD_ID,
                                    add => add.ADD_ID,
                                    (emp2, add)=>new {emp2, add}
                                    )
                              .Select(
                                        e => new 
                                        {
                                            ID = e.emp2.emp1.ID,
                                            Name = e.emp2.emp1.Name,
                                            Salary = e.emp2.emp1.Salary,
                                            DeptName = e.emp2.dept.DEPT_Name,
                                            address = e.add.address
                                        }).ToList();
                
        Console.WriteLine("Employee Details: ");
        foreach (var e in Result)
        {
            Console.WriteLine("\tID: " + e.ID + ", Name: " + e.Name + ", Salary: " + e.Salary + ", Department: " + e.DeptName+", Address: "+e.address);
        }
    }
}
Employee Details:
        ID: 101, Name: Amit  , Salary: 4000, Department: HR        , Address: AGRA
        ID: 102, Name: Amit  , Salary: 3800, Department: ACCOUNTS  , Address: BAMBAI
        ID: 103, Name: Salman, Salary: 3500, Department: SALES     , Address: CHENNAI
        ID: 104, Name: Ram   , Salary: 2000, Department: HR        , Address: DELHI
        ID: 105, Name: Shyam , Salary: 7000, Department: ACCOUNTS  , Address: GWALIOR
        ID: 106, Name: Kishor, Salary: 5000, Department: SALES     , Address: JHASHI
Press any key to continue . . .

Final Words

What we learned through this article is C# Program to Join Multiple Data Sources Using LINQ Join() Method. Also if you have any doubts please leave a comment via the comment box. And we ask that you benefit everyone who shared this article with your friends.    

Hi, I'm Ranjith a full-time Blogger, YouTuber, Affiliate Marketer, & founder of Coding Deekshi. Here, I post about programming to help developers.

Share on:

Leave a Comment