I create this class for a test.
I want to compare to List of the class and get different class between ListA and ListB. In my example the result get only class of ListB.
I do the same thin with list of string and work it
Class Example
public class FileNode
{
public string Source { get; set; }
public int Id { get; set; }
}
List<FileNode> ListA = new List<FileNode>
{
new FileNode{ Id = 1, Source="a" },
new FileNode{ Id = 2, Source="b" },
};
List<FileNode> ListB = new List<FileNode>
{
new FileNode{ Id = 1, Source="a" },
new FileNode{ Id = 2, Source="b" },
new FileNode{ Id = 3, Source="c" },
};
List<FileNode> ListAB = ListB.Where(m => !ListA.Contains(m)).ToList();
String example, it’s works
List<string> a = new List<string> {"a","b","c","d","e" };
List<string> b = new List<string> {"a","b","c","d" };
List<string> ab = a.Where(m => !b.Contains(m)).ToList();
Well
Containsis going to callEqualson the elements – and may also useGetHashCode(I doubt it, but you should override it consistently anyway). So you need to overrideEquals(object)andGetHashCode()inFileNode. (By default, you’ll get reference equality.)Note that as soon as you start trying to use
Containsin a query which will execute in the database, it could behave completely different – it wouldn’t be looking at yourEquals/GetHashCodemethods at that point.