C# program to join Employee and Department class using Linq Join Method

Here, we are going to learn how to join Employee and Department class using Linq Join Method in C#?
Submitted by Nidhi, on September 01, 2020

Here we will create two classes Employee and Department. Then we will join both Employee and Department based on department id using Linq Join() method.

Program:

The source code to join the Employee and Department using Linq Join Method, which is given below. The given program is compiled and executed successfully on Microsoft Visual Studio.

//C# program to join Employee and 
//Department class 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 string   Name    ;
}

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

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},
             new Employee {ID=102,   Name="Amit  "    , Salary=3800,DEPT_ID=102},
             new Employee {ID=103,   Name="Salman"    , Salary=3500,DEPT_ID=103},
             new Employee {ID=104,   Name="Ram   "    , Salary=2000,DEPT_ID=101},
             new Employee {ID=105,   Name="Shyam "    , Salary=7000,DEPT_ID=102},
             new Employee {ID=106,   Name="Kishor"    , Salary=5000,DEPT_ID=103},
        };

        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     "   },
        };

        var Result = employees.Join(departments,  
                           emp => emp.DEPT_ID, 
                           dept => dept.DEPT_ID, 
                           (emp, dept) => new 
                           {
                               ID      = emp.ID     ,
                               Name    = emp.Name   ,
                               Salary  = emp.Salary ,
                               DeptName = dept.DEPT_Name

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

Output:

Employee Details:
        ID: 101, Name: Amit  , Salary: 4000, Department: HR
        ID: 102, Name: Amit  , Salary: 3800, Department: ACCOUNTS
        ID: 103, Name: Salman, Salary: 3500, Department: SALES
        ID: 104, Name: Ram   , Salary: 2000, Department: HR
        ID: 105, Name: Shyam , Salary: 7000, Department: ACCOUNTS
        ID: 106, Name: Kishor, Salary: 5000, Department: SALES
Press any key to continue . . .

Explanation:

In the above program, we created three classes Employee, Department, and JoinDemo. Employee class contains data members ID, Name, Salary, and DEPT_ID. The Department class contains DEPT_ID and DEPT_Name.

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

var Result = employees.Join(departments,  
                emp => emp.DEPT_ID, 
                dept => dept.DEPT_ID, 
                (emp, dept) => new 
                {
                    ID      = emp.ID     ,
                    Name    = emp.Name   ,
                    Salary  = emp.Salary ,
                    DeptName = dept.DEPT_Name

                }).ToList();

In the above code, we joined the Employee and Department class using Join() method and select ID, Name, Salary, and DEPT_Name.

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

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






Comments and Discussions

Ad: Are you a blogger? Join our Blogging forum.





Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates


© https://www.includehelp.com some rights reserved.