简体   繁体   中英

Filter List based on another list

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?

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));

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;
             });

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM