Filter List based on another list

72 Views Asked by At

i have two lists based on other objects.

List<Emyployee> emyployeeList;
List<Display> displayEmployeeList;

both of them have the id's of the employees, but the second list only have a few of them. I want to filter the employeeList that i have all id's that arent in the displayEmployeeList.

how can i do that?

2

There are 2 best solutions below

0
On

If displayEmployeeList has many items you can find useful to create a kind of index (like that of RDBMS):

  // let id be integer
  HashSet<int> ids = new HashSet<int>(displayEmployeeList
    .Select(item => item.id)
  );

  // Just Linq where
  var result = emyployeeList
    .Where(item => !ids.Contains(item.id));
0
On

You could use the Zip extension method and do it like the following:

employeeList.Zip(displayEmployeeList,(employee,display) => 
             {
                if(employee.Id != display.Id)
                  return employee;
             });