I am trying accomplish the LINQ query below but I need a “not equal” instead of equal, so that filteredEmployees has all employees from groupA minus groupB.
List<Employee> groupA = getEmployeeA();
List<Employee> groupB = getEmployeeB();
var filteredEmployees = from a in groupA
join b in groupB on a.Name equals b.Name
select a;
You don’t need a join for that:
Note that this will be a sequence of unique employees – so if there are any duplicates in
groupA, they will only appear once infilteredEmployees. Of course, it also assumes you’ve got a reasonable equality comparer1. If you need to go specifically on name, you can useExceptByfrom MoreLINQ:Or without going into a third party library:
1 As pointed out in the comments, you can pass in an
IEqualityComparer<T>as an argument toExcept. I have aProjectionEqualityComparerclass in MiscUtil which makes it easy to build a comparer of the kind you need: