You can use Except
. Given that you have a custom type, though, you'll want to override Equals()
and GetHashCode()
so that it works well with Linq.
This is because Linq strives for efficiency, so it can use HashSet<T>
and other mechanisms to quickly determine if items are possibly equivalent or not (items that are equivalent must have same hash code, or your hash code is bad). So it's not just enough to implement Equals()
, you must also implement GetHashCode()
when using methods of this type with custom classes.
For example:
public class tobj : IEquatable<tobj>
{
public string column1;
public string column2;
public bool Equals(tobj other)
{
return other != null ? Equals(column1, other.column1) && Equals(column2, other.column2) : false;
}
public override bool Equals(object obj)
{
return Equals(obj as tobj);
}
public override int GetHashCode()
{
// seed the code
int hash = 13;
// use some primes to mix in the field hash codes...
hash = hash * 17 + column1.GetHashCode();
hash = hash * 17 + column2.GetHashCode();
return hash
}
}
Now that you have GetHashCode() and Equals() overridden (I like implementing IEquatable<T>
myself as well), you can use Except()
:
var results = array1.Except(array2);
foreach (var i in results)
{
Console.WriteLine(i.column1 + ":" + i.column2);
}
I'd recommend seeing this SO thread on algorithms for hash codes as well for tips on implementing a good one.
Also, as a side note, some types (such as KeyValuePair
, Tuple
, and anonymous types) already implement Equals()
and GetHashCode()
correctly for equivalence in such a way that they take into account their individual components.