Entities
public class Department
{
public int DeptID {get; set;}
public string DeptName {get;set;}
}
public class EmpDept
{
public int DeptID { get;set;}
public string DeptName {get;set;}
public List < Employee > employee {get;set;}
}
public class Employee
{
public int EmpID {get;set;}
public int DeptID {get;set;}
public string Name {get;set;}
}Sample Data for Employee and Department entities object
List < Department > objDeptCollection = new List < Department > () {
new Department() {
DeptID = 1, DeptName = "Finance"
}, new Department() {
DeptID = 2, DeptName = "L&D"
}, new Department() {
DeptID = 3, DeptName = "HR"
}, new Department() {
DeptID = 4, DeptName = "DEV"
}
};
List < Employee > objEmpCollection = new List < Employee > () {
new Employee() {
DeptID = 1, EmpID = 11, Name = "Sudheer"
}, new Employee() {
DeptID = 1, EmpID = 12, Name = "Suneel"
}, new Employee() {
DeptID = 2, EmpID = 13, Name = "Ramu"
}, new Employee() {
DeptID = 2, EmpID = 14, Name = "Pavani"
}, new Employee() {
DeptID = 3, EmpID = 15, Name = "Ishita"
}, new Employee() {
DeptID = 4, EmpID = 16, Name = "Yagna"
}, new Employee() {
DeptID = 1, EmpID = 17, Name = "Sravanthi"
}, new Employee() {
DeptID = 3, EmpID = 18, Name = "Kyathi"
}
};LINQ Query to map the objects into single Parent and child object
List < EmpDept > objBoth = new List < EmpDept > ();
objBoth = (from emp in objEmpCollection
join dept in objDeptCollection on emp.DeptID equals dept.DeptID
select(new EmpDept {
DeptID = dept.DeptID, employee = new List < Employee > {
new Employee {
DeptID = emp.DeptID, Name = emp.Name, EmpID = emp.EmpID
}
}
})).ToList();