The source code to join Employee and Department using Linq Join Query, 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 Query.usingSystem;usingSystem.Linq;usingSystem.Collections.Generic;publicclassEmployee{publicint ID;publicint Salary;publicint DEPT_ID;publicstring Name;}publicclassDepartment{publicint DEPT_ID;publicstring DEPT_Name;}publicclassJoinDemo{staticvoidMain(string[] args){List<Employee> employees =newList<Employee>(){newEmployee{ID=101, Name="Amit ", Salary=4000,DEPT_ID=101},newEmployee{ID=102, Name="Amit ", Salary=3800,DEPT_ID=102},newEmployee{ID=103, Name="Salman", Salary=3500,DEPT_ID=103},newEmployee{ID=104, Name="Ram ", Salary=2000,DEPT_ID=101},newEmployee{ID=105, Name="Shyam ", Salary=7000,DEPT_ID=102},newEmployee{ID=106, Name="Kishor", Salary=5000,DEPT_ID=103},};List<Department> departments =newList<Department>(){newDepartment{DEPT_ID=101, DEPT_Name="HR "},newDepartment{DEPT_ID=102, DEPT_Name="ACCOUNTS "},newDepartment{DEPT_ID=103, DEPT_Name="SALES "},};var ResultQuery =(from emp in employees
join dept in departments
on emp.DEPT_ID equals dept.DEPT_ID
selectnew{
ID = emp.ID,
Name = emp.Name,
Salary = emp.Salary,
DeptName = dept.DEPT_Name
}).ToList();
Console.WriteLine("Employee Details: ");foreach(var e in ResultQuery){
Console.WriteLine("\tID: "+ e.ID +", Name: "+ e.Name +", Salary: "+ e.Salary +", Department: "+ e.DeptName);}}}
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 ResultQuery = (from emp in employees
join dept in departments
on emp.DEPT_ID equals dept.DEPT_ID
select new
{
ID = emp.ID,
Name = emp.Name,
Salary = emp.Salary,
DeptName = dept.DEPT_Name
}).ToList();
In the above code we joined Employee and Department class using join query and select ID, Name, Salary, and DEPT_Name.
Console.WriteLine("Employee Details: ");
foreach (var e in ResultQuery)
{
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.
Program:
The source code to join Employee and Department using Linq Join Query, which is given below. The given program is compiled and executed successfully on Microsoft Visual Studio.
Output:
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.
In the above code we joined Employee and Department class using join query and select ID, Name, Salary, and DEPT_Name.
The above code will print the result of join on the console screen.
need an explanation for this answer? contact us directly to get an explanation for this answer