无法在 Entity Framework 的 LINQ 中使用 IN 查询
Not able to use IN query in LINQ with Entity Framework
我正在使用 EF Framework 从 SQL 数据库检索数据。
子请求 Table 如下所示:
在此table中"org_assigneddept"是另一个部门的外键Table。
我有部门列表作为输入,我只想从数据库中检索 org_assigneddept 与列表匹配的那些行。
请找到我的全部代码:-
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: 'Unable to create a constant value of
type 'Application.Business.DLL.EmpRoleDeptViewModel'. Only primitive
types or enumeration types are supported in this context.'
请告诉我如何才能达到结果
您不能将部门 VM 传递给 SQL,它不知道这些是什么。
// Extract the IDs from the view models.. Now a list of primitive types..
var departmentIds = vmDept.Select(x => x.DeptId).ToList();
然后在您的 select 语句中...
..
where departmentIds.Contains(id=> id == ers.org_assigneddept)
..
我正在使用 EF Framework 从 SQL 数据库检索数据。
子请求 Table 如下所示:
在此table中"org_assigneddept"是另一个部门的外键Table。
我有部门列表作为输入,我只想从数据库中检索 org_assigneddept 与列表匹配的那些行。
请找到我的全部代码:-
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: 'Unable to create a constant value of type 'Application.Business.DLL.EmpRoleDeptViewModel'. Only primitive types or enumeration types are supported in this context.'
请告诉我如何才能达到结果
您不能将部门 VM 传递给 SQL,它不知道这些是什么。
// Extract the IDs from the view models.. Now a list of primitive types..
var departmentIds = vmDept.Select(x => x.DeptId).ToList();
然后在您的 select 语句中...
..
where departmentIds.Contains(id=> id == ers.org_assigneddept)
..