C# - Join multiple data sources using LINQ

Learn, how to join multiple data sources using Linq in C#? By Nidhi Last updated : April 01, 2023

Here, we will create three classes Employee, Department, and Address. Then we will join Employee and Department based on department id and join Employee with Address based on address id.

C# program to join multiple data sources using LINQ

The source code to join multiple data sources using Linq, which is given below. The given program is compiled and executed successfully on Microsoft Visual Studio.

// C# - Join multiple data sources using LINQ.

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 ResultQuery = (from emp in employees

      join dept in departments on emp.DEPT_ID equals dept.DEPT_ID

      join add in addresses on emp.ADD_ID equals add.ADD_ID

      select new {
        ID = emp.ID,
          Name = emp.Name,
          Salary = emp.Salary,
          DeptName = dept.DEPT_Name,
          address = add.address
      }
    ).ToList();

    Console.WriteLine("Employee Details: ");
    foreach(var e in ResultQuery) {
      Console.WriteLine("\tID: " + e.ID + ", Name: " + e.Name + ", Salary: " + e.Salary + ", Department: " + e.DeptName + ", Address: " + e.address);
    }
  }
}

Output

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 . . .

Explanation

In the above program, we created four classes Employee, Department, Address, and JoinDemo. Employee class contains data members ID, Name, Salary, and DEPT_ID. Department class contains DEPT_ID and DEPT_Name and Address class contains ADD_ID and address.

Now look to the JoinDemo class, the JoinDemo class contains the Main() method. In the Main() method we created a list of employees, departments, and addresses.

var ResultQuery = (from emp in employees

                join dept in departments
                on emp.DEPT_ID equals dept.DEPT_ID

                join add in addresses
                on emp.ADD_ID equals add.ADD_ID

                select new
                {
                    ID       = emp.ID        ,
                    Name     = emp.Name      ,
                    Salary   = emp.Salary    ,
                    DeptName = dept.DEPT_Name,
                    address  = add.address
                }
        ).ToList();

In the above code, we joined the Employee, Department, and Address class based on DEPT_ID and ADD_ID.

Console.WriteLine("Employee Details: ");
foreach (var e in ResultQuery)
{
    Console.WriteLine("\tID: " + e.ID + ", Name: " + e.Name + ", Salary: " + e.Salary + ", Department: " + e.DeptName+", Address: "+e.address);
}

The above code will print the result of join on the console screen.

C# LINQ Programs »




Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.