Как я могу применить внешнее соединение с помощью LINQ в mvc. Внутреннее соединение работает, но внешнее соединение дает следующую ошибку..почему?
эта ошибка приближается..Может ли кто-нибудь попытаться ее решить?
An exception of type 'System.NullReferenceException' occurred in App_Web_linqquery.cshtml.a8d08dba.0r5s74cf.dll but was not handled in user code<br /> <br /> Additional information: Object reference not set to an instance of an object.
Что я уже пробовал:
namespace WebApplication4Linq.Models { public class Employee { [Key] public int EmpId { get; set; } public string EmpName { get; set; } public string EmpCode { get; set; } public string DeptId { get; set; } public static List<employee> GetEmployees() { List<employee> employees = new List<employee>(); employees.Add(new Employee { EmpId = 1, EmpName = "Shyam", EmpCode = "AAA", DeptId = "D01" }); employees.Add(new Employee { EmpId = 2, EmpName = "Raja", EmpCode = "BBB", DeptId = "D03" }); employees.Add(new Employee { EmpId = 3, EmpName = "Hari", EmpCode = "CCC", DeptId = "D04" }); employees.Add(new Employee { EmpId = 4, EmpName = "Krushna", EmpCode = "DDD", DeptId = "D03" }); employees.Add(new Employee { EmpId = 5, EmpName = "Mohan", EmpCode = "EEE", DeptId = "D02" }); return employees; } } }
--------------
namespace WebApplication4Linq.Models { public class Department { [Key] public string DeptName { get; set; } public string DeptCode { get; set; } public string DeptId { get; set; } public static List<department> GetDepts() { List<department> depts = new List<department>(); depts.Add(new Department { DeptName = "CS", DeptCode = "ZZ", DeptId = "D01" }); depts.Add(new Department { DeptName = "EEE", DeptCode = "XX", DeptId = "D02" }); depts.Add(new Department { DeptName = "MCA", DeptCode = "YY", DeptId = "D03" }); return depts; } } }
-----------------------модель представления=----------
namespace WebApplication4Linq.Models { public class ViewModel { public Employee employees { get; set; } public Department depts { get; set; } } } -------------Controller--------------------- public class HomeController : Controller { public ActionResult LinqQuery() { ViewModel mymodel = new ViewModel(); List<employee> emps = Employee.GetEmployees(); List<department> dept = Department.GetDepts(); var outerjoin = (from empn in emps join den in dept on empn.DeptId equals den.DeptId into abc from den in abc.DefaultIfEmpty() select new ViewModel() { employees = empn, depts = den }); return View(outerjoin);
-------------------(.cshtml)------------------
@model IEnumerable<webapplication4linq.models.viewmodel> @using (Html.BeginForm()) { <h2>Index Page</h2> <hr /> <h2>Employee Data</h2><hr /> <table> <tr> <th>EmpName</th> <th>EmpCode</th> <th>DeptName</th> <th>DeptCode</th> </tr> @foreach(var emp in Model) { <tr> <td>@emp.employees.EmpName</td> <td>@emp.employees.EmpCode</td> <td>@emp.depts.DeptName</td> <td>@emp.depts.DeptCode</td> </tr> } </table> }