VOOZH about

URL: https://www.geeksforgeeks.org/c-sharp/c-sharp-program-to-join-employee-and-department-class-using-linq-join-query/

⇱ C# Program to Join Employee and Department Class using LINQ Join Query - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

C# Program to Join Employee and Department Class using LINQ Join Query

Last Updated : 23 Jul, 2025

Given two classes named as Employee and Department, now we join Employee and Department class with the help of LINQ join Query. So to this task, we use the Join clause. This clause is used to join two data sources into one source which has some common attributes. It always takes two data sources and the result of the join depends upon which type o join is used like inner join, cross join, left outer join, and group join.

Example:

Input:

Student

new Employee{id = 7058, name = "sravan kumar", dept_id = 1, add_id = 21},

new Employee{id = 7059, name = "jyothika", dept_id = 2, add_id = 22},

new Employee{id = 7072, name = "harsha", dept_id = 1, add_id = 22},

new Employee{id = 7076, name = "khyathi", dept_id = 4, add_id = 27},

Department

new Department{dept_id = 1, dept_name = "CSE"},

new Department{dept_id = 2, dept_name = "CSE"},

new Department{dept_id = 3, dept_name = "IT"},  

Address

new Address{add_id = 21, address_name = "hyd"},

new Address{add_id = 22, address_name = "railu-peta"},

new Address{add_id = 24, address_name = "chenchu-peta"},  

Output:

ID: 7058--> Name: sravan kumar--> Department: CSE--> Address: hyd

ID: 7059--> Name: jyothika--> Department: CSE--> Address: railu-peta

ID: 7072--> Name: harsha--> Department: CSE--> Address: railu-peta

Approach:

1. Create two data sources by using a list named Employee, and Department by declaring the variables.

2. Add values to these lists.

3. Perform the join based on student id, and department id.

var result = (from stu in employees
join dept in departments on stu.dept_id equals dept.dept_id).ToList();

4. Select the data using select() method.

select new
{
 ID = stu.id, Name = stu.name,
 DeptName = dept.dept_name,
}

5. Display the output using for each loop.

foreach(var e in result)
{
 Console.WriteLine("ID: " + e.ID + 
 "--> Name: " + e.Name + 
 "--> Department: " + e.DeptName );
}

Example:

Output:

ID: 234--> Name: sravan kumar--> Department: CSE
ID: 244--> Name: Monika--> Department: CSE
ID: 734--> Name: harsha--> Department: CSE
Comment
Article Tags:
Article Tags:

Explore