Inner Join using Lambda Expression and Linq
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace GroupJoin
{
class Program
{
public static void Main()
{
List<Employee> objEmployeelist =
new List<Employee>
{
new Employee{ EmployeeID=1, EmployeeName ="Anurag", DepID=1},
new Employee{ EmployeeID=2, EmployeeName ="Abhishek",DepID=2},
new Employee{ EmployeeID=3, EmployeeName ="Rahul", DepID=6}
};
List<Department> objDepartmentlist = new List<Department>
{
new Department {DepartmentID=1,DepartmentName="HR"},
new Department {DepartmentID=2,DepartmentName="Admin"},
new Department {DepartmentID=3,DepartmentName="Lib"},
new Department {DepartmentID=4,DepartmentName="Abc"}
};
var listlinq = from e in objEmployeelist
join d in objDepartmentlist
on e.DepID equals d.DepartmentID
select new { emp = e, dep = d };
var listlambda = objEmployeelist.Join(objDepartmentlist, e => e.DepID, d => d.DepartmentID,
(e, d) => new { emp = e, dep = d });
foreach (var item in listlambda)
{
Console.WriteLine(item.emp.EmployeeName + "::" + item.dep.DepartmentName);
}
Console.ReadLine();
}
}
public class Employee
{
public int EmployeeID;
public string EmployeeName;
public int DepID;
}
public class Department
{
public int DepartmentID;
public string DepartmentName;
}
}