Linq Except considering only one property
c#, linq
Solution
The `Except` method requires that the two collection types involved have the same element type. In this case the element types are different (`object1` and `object2`) hence `Except` isn't really an option. A better method to use here is `Where`
obj2 = obj2
.Where(x => !obj1.Any(y => y.StringProperty == x.StringProperty))
.ToList();
Problem
I have two lists of object. ``` List<object1> obj1 = new List<object1>(); List<object2> obj2 = new List<object2>(); ``` I want to do this: ``` obj2 = obj2.Except(obj1).ToList(); ``` However, by reading other questions similar to mine, I understand that this doesn't work unless I override Equals. I do not want to do that, but both obj2 and obj1 have a string property that is sufficient to see whether they are equal. If `obj2.StringProperty` is equivalent to `obj1.StringProperty` then the two can be considered equal. Is there any way I can use Except, but by using only the string property to compare?