Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>First of all this is wrong:</p> <pre><code>public bool Equals(MyClass item1, MyClass item2) { return GetHashCode(item1) == GetHashCode(item2); } </code></pre> <p>If the hashcode's are different for sure the corresponding 2 items are different, but if they're equal is not guaranteed that the corresponding 2 items are equal.</p> <p>So this is the correct <code>Equals</code> implementation:</p> <pre><code>public bool Equals(MyClass item1, MyClass item2) { if(object.ReferenceEquals(item1, item2)) return true; if(item1 == null || item2 == null) return false; return item1.MyString1.Equals(item2.MyString1) &amp;&amp; item1.MyString2.Equals(item2.MyString2); } </code></pre> <p>As <a href="https://stackoverflow.com/questions/4340273/intersect-with-a-custom-iequalitycomparer-using-linq/4340318#4340318">Slacks suggested</a> (anticipating me) the code is the following:</p> <pre><code>var Default = new List&lt;MyClass&gt; { new MyClass{MyString1="A",MyString2="A",MyString3="-"}, new MyClass{MyString1="B",MyString2="B",MyString3="-"}, new MyClass{MyString1="X",MyString2="X",MyString3="-"}, new MyClass{MyString1="Y",MyString2="Y",MyString3="-"}, new MyClass{MyString1="Z",MyString2="Z",MyString3="-"}, }; var Good = new List&lt;MyClass&gt; { new MyClass{MyString1="A",MyString2="A",MyString3="+"}, new MyClass{MyString1="B",MyString2="B",MyString3="+"}, new MyClass{MyString1="C",MyString2="C",MyString3="+"}, new MyClass{MyString1="D",MyString2="D",MyString3="+"}, new MyClass{MyString1="E",MyString2="E",MyString3="+"}, }; var wantedResult = Good.Intersect(Default, new MyEqualityComparer()) .Union(Default, new MyEqualityComparer()); // wantedResult: // A A + // B B + // X X - // Y Y - // Z Z - </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload