[英]Use LINQ to get items in one List<>, that are in another List<>
[英]Use Linq to get Items in List that do not have a match in another list
我有一个雇员列表和一个employeeGroups列表。 我正在尝试获取在employeeGroups列表中没有匹配记录的员工列表。 我尝试使用这里找到的示例,但它不能满足我的需要,我不知道如何更改它以获取所需的列表。 结果始终为NULL。
这是我的代码:
List<ModuleView.EmployeeBO> employees = EmployeeBA.Employee_GetList_All(DB_Context, IsActiveChoice.Active, IsEnabledChoice.Enabled);
List<PortalView.EmployeeGroupBO> groups = SecurityDA.EmployeeGroup_GetList_All(DB_Context);
List<ModuleView.EmployeeBO> result = employees.Where(p2=> !groups.Any( p=>p.EmployeeId == p2.EmployeeId)) as List<ModuleView.EmployeeBO>;
return result;
非常感谢您的协助!
这里的问题是as List<ModuleView.EmployeeBO>
。 .Where()
方法始终返回延迟计算的IEnumerable<>
,而不是List<>
。 要使其正常工作,只需将其更改为.ToList()
。
您可以尝试一下。 我没有机会测试代码。
List<ModuleView.EmployeeBO> employees = EmployeeBA.Employee_GetList_All(DB_Context, IsActiveChoice.Active, IsEnabledChoice.Enabled);
List<PortalView.EmployeeGroupBO> groups = SecurityDA.EmployeeGroup_GetList_All(DB_Context);
List<ModuleView.EmployeeBO> result = employees.Where(p2=> !groups.Any( p=>p.EmployeeId == p2.EmployeeId)).ToList();
return result;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.