无法在LINQ中使用实体框架中的IN查询

时间:2017-07-21 07:25:00

标签: entity-framework linq linq-to-entities

我正在使用EF Framework从SQL DB中检索数据。

子请求表如下所示:

enter image description here

在此表中" org_assigneddept"是另一个部门表的外键。

我有Departments列表作为输入,我想只从org_assigneddept与列表匹配的DB中检索那些行。

请找到我的整个代码: -

   private List<EventRequestDetailsViewModel> GetSummaryAssignedDeptEventRequests(List<EmpRoleDeptViewModel> vmDept)
            {
                List<EventRequestDetailsViewModel> vmEventRequestDeptSummary = new List<EventRequestDetailsViewModel>();

                RequestBLL getRequestBLL = new RequestBLL();
                Guid subRequestStatusId = getRequestBLL.GetRequestStatusId("Open");

                using (var ctxGetEventRequestSumm = new STREAM_EMPLOYEEDBEntities())
                {
                    vmEventRequestDeptSummary = (from ers in ctxGetEventRequestSumm.SubRequests                                                                                    
                                                 where vmDept.Any(dep=>dep.DeptId == ers.org_assigneddept)  
                                                 select new EventRequestDetailsViewModel
                                                 {
                                                     SubRequestId = ers.org_subreqid
                                                 }).ToList();
                }
           }

它在LINQ查询级别给出以下错误: -

  

System.NotSupportedException:&#39;无法创建常量值   键入&#39; Application.Business.DLL.EmpRoleDeptViewModel&#39;。只有原始   在此上下文中支持类型或枚举类型。&#39;

请告诉我如何实现结果

1 个答案:

答案 0 :(得分:1)

您无法将部门虚拟机传递给SQL,也不知道它们是什么。

// Extract the IDs from the view models.. Now a list of primitive types..
var departmentIds = vmDept.Select(x => x.DeptId).ToList();

然后在你的选择陈述......

..
where departmentIds.Contains(id=> id == ers.org_assigneddept)
..