Reputation: 16959
I have two lists with different objects. Both objects have a string property name that I need for comparing.
I need to know what values in list A are not contained in list B based on the name property.
Would the except operator work for this case? What would be a optimal way to achive this in Linq?
Upvotes: 1
Views: 26
Reputation: 727047
Except
operator removes items based on object equality. Although you could shoehorn your scenario into Except
by passing an "equality comparer" that pays attention only to Name
property, the resultant code would be hard to understand.
A better approach is to make a set of names that you wish to exclude, and use that set in your query:
var excludedNames = new HashSet<string>(listB.Select(item => item.Name));
var result = listA.Where(item => !excludedNames.Contains(item.Name)).ToList();
Upvotes: 1