C# Program to Join Employee and Department Class using LINQ Join Query
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:
C#
// C# program to Join Employee and Department Class // using LINQ Join Query using System; using System.Linq; using System.Collections.Generic; // Variables for Employee list public class Employee { public int id; public string name; public int dept_id; public int add_id; } // Variables for Department list public class Department { public int dept_id; public string dept_name; } // Variables for Address list public class Address { public int add_id; public string address_name; } class GFG{ // Driver code static void Main( string [] args) { // Enter data for Employee list List<Employee> employees = new List<Employee>() { new Employee{ id = 234, name = "sravan kumar" , dept_id = 1}, new Employee{ id = 244, name = "Monika" , dept_id = 2}, new Employee{ id = 734, name = "harsha" , dept_id = 1}, new Employee{ id = 533, name = "komal" , dept_id = 4}, }; List<Department> departments = new List<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 " }, }; // Join the employees and other two tables var result = ( from stu in employees join dept in departments on stu .dept_id equals dept .dept_id select new { ID = stu.id, Name = stu.name, DeptName = dept.dept_name, }).ToList(); // Display the result foreach ( var e in result) { Console.WriteLine( "ID: " + e.ID + "--> Name: " + e.Name + "--> Department: " + e.DeptName ); } } } |
Output:
ID: 234--> Name: sravan kumar--> Department: CSE ID: 244--> Name: Monika--> Department: CSE ID: 734--> Name: harsha--> Department: CSE
Please Login to comment...