The problem is that the getKeyValues selector you provide will return a List
from each Component
to compare on. The returned List
from each will be compared by reference, so essentially you've got this going on:
var listA = new List<string> { "SomeString" };
var listB = new List<string> { "SomeString" };
bool areListsEqual = listA == listB;
areListsEqual
will return false, since they are compared by reference. Essentially what you'll need is either a different EqualityComparer
(available to add via an overload of GroupJoin
), or you need a way to compare the properties by value.
An example of something that would work (but not necessarily a good way to do it) would be:
Func<Component, string> getKeyValues = x =>
string.Join(",", x.Attributes
.Where(a => _keyAttributes.Contains(a.Name))
.OrderBy(a => a.Name)
.Select(a => a.Value).ToArray());
This will create a string that represents the values in each list, and that will be used for comparison. A better way would be to use an EqualityComparer
that has your own logic for what makes the lists actually equal based on the values contained within. See here for how you might compare two lists.