0

I have a list of employees, and a list of employeeGroups. I am trying get a list of employees that do not have a matching record in the employeeGroups list. I tried using the example found HERE but it is not working for my needs and I can't figure out how to change it to get the desired list. The result is always NULL.

Here is my code:

        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;

Any assistance is greatly appreciated!

Rani Radcliff
  • 4,856
  • 5
  • 33
  • 60

2 Answers2

2

The problem here is the as List<ModuleView.EmployeeBO>. The .Where() method always returns an IEnumerable<> that's lazily evaluated, not a List<>. To get this to work, simply change that to .ToList().

Kyle Sletten
  • 5,365
  • 2
  • 26
  • 39
0

You might try this. I did not get a chance to test the code.

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;
Shammas
  • 381
  • 1
  • 4
  • 15