kls dngr

kls dngr

  • NA
  • 17
  • 5.4k

How to display two class result using LINQ to view in MVC?

Aug 6 2015 5:59 AM
Here is my controller action method code but I don't have any idea to display that result to view?
public class DetailsController : Controller
{
//
// GET: /Details/
List<Employee> employees = new List<Employee>();
List<Department> departments = new List<Department>();
public ActionResult Index()
{
employees.Add(new Employee { DepartmentID = 1, EmployeeID = 1, EmployeeName = "Kailas" });
employees.Add(new Employee { DepartmentID = 2, EmployeeID = 2, EmployeeName = "Dipak" });
employees.Add(new Employee { DepartmentID = 2, EmployeeID = 3, EmployeeName = "Pramod" });
employees.Add(new Employee { DepartmentID = 1, EmployeeID = 4, EmployeeName = "Prakash" });
employees.Add(new Employee { DepartmentID = 3, EmployeeID = 5, EmployeeName = "Pranav" });
employees.Add(new Employee { DepartmentID = 4, EmployeeID = 6, EmployeeName = "Dipesh" });
departments.Add(new Department { DepartmentID = 1, DepartmentName = "IT" });
departments.Add(new Department { DepartmentID = 1, DepartmentName = "BA" });
departments.Add(new Department { DepartmentID = 1, DepartmentName = "CS" });
departments.Add(new Department { DepartmentID = 1, DepartmentName = "MBA" });
departments.Add(new Department { DepartmentID = 1, DepartmentName = "Comm" });
var list = (from e in employees
join d in departments
on e.DepartmentID equals d.DepartmentID
select new {
EmployeeName = e.EmployeeName,
DepartmentName = d.DepartmentName
}).ToList();
return View(list);
}
}
 

Answers (3)