显示无法创建类型''的常量值。在此上下文中仅支持原始类型(例如Int32,String和Guid')

时间:2014-01-29 16:10:24

标签: c# linq

我正在查询一些表格,以便根据使用linq的某些条件获取员工列表。如

此类“EmpJobPosition”来自Model。

List<int> empjList=ObjToList(employeeJobPositionIds);

List<EmpJobPosition> empJobPositionList =
     (from i in ctx.EmpJobPositions
      where empjList.Contains(i.EmpJobPositionId)
      select i).ToList<EmpJobPosition>();

var query = (from emp in ctx.Employees
     join resg in ctx.Resignations on emp.EmployeeID equals resg.EmployeeID into resglist
     from resg in resglist.DefaultIfEmpty()
     join jpos in empJobPositionList
         on emp.EmployeeID equals jpos.EmployeeId into jposList
     from jpos in jposList.DefaultIfEmpty()
         (resg == null || resg.HasLeft == false) && emp.CompanyID == 1
     select new EmployeesViewModel()).ToList();

但是在这里与empJobPositionList连接时,它是showig错误,如

  

{“无法创建类型为'Etisbew.eOffice.EFModel.EntityModel.EmpJobPosition'的常量值。在此上下文中仅支持基本类型(例如Int32,String和Guid')。”}

这里有什么问题。

1 个答案:

答案 0 :(得分:1)

你可以这样做(不要试图加入列表上的IQueryable)

var query = (
     from emp in ctx.Employees
     join resg in ctx.Resignations 
              on emp.EmployeeID equals resg.EmployeeID into resglist
     from leftresg in resglist.DefaultIfEmpty()

     //put the where clause on EmpJobPositions here
     join jpos in ctx.EmpJobPositions.Where(x => empjList.Contains(x.EmpJobPositionId))
              on emp.EmployeeID equals jpos.EmployeeId into jposList
     from leftjpos in jposList.DefaultIfEmpty()
         //don't understand this line, are you missing a where ?
         //(leftresg == null || leftresg.HasLeft == false) && emp.CompanyID == 1
     select new EmployeesViewModel()).ToList();