You can do something like this:
List<Device> devices = ... List<DeviceInfo> deviceInfos = ... var deviceIds = devices.Select(d => d.Id) .OrderBy(id => id); var deviceInfoIds = deviceInfos.Select(d => d.Identifier) .OrderBy(id => id); bool areEqual = deviceIds.SequenceEqual(deviceInfoIds);
If duplicate identifiers are not possible, specify the semantics:
bool areEqual = !devices.Select(d => d.Id) .Except(deviceInfos.Select(d => d.Identifier)) .Any();
I would recommend, if possible, that you declare an IHasId interface (or similar) and get both types to implement it.
EDIT
In response to your editing, you can write an implementation of IEqualityComparer that did what you wanted. It would look very ugly; you will need to make a speculative selection from each argument in DeviceInfo / Device in order to try to extract the identifier. I would not recommend this; it is a bad idea to compare equality to compare objects of completely different types. It would be much simpler if each type implements a common interface that provides an identifier.
source share