Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>For explanation, take a look at other answers. I'm just providing one way to handle this issue.</p> <p>You might like <a href="http://brendan.enrick.com/post/LINQ-Your-Collections-with-IEqualityComparer-and-Lambda-Expressions.aspx" rel="nofollow">this</a>:</p> <pre><code>public class LambdaComparer&lt;T&gt;:IEqualityComparer&lt;T&gt;{ private readonly Func&lt;T,T,bool&gt; _comparer; private readonly Func&lt;T,int&gt; _hash; public LambdaComparer(Func&lt;T,T,bool&gt; comparer): this(comparer,o=&gt;0) {} public LambdaComparer(Func&lt;T,T,bool&gt; comparer,Func&lt;T,int&gt; hash){ if(comparer==null) throw new ArgumentNullException("comparer"); if(hash==null) throw new ArgumentNullException("hash"); _comparer=comparer; _hash=hash; } public bool Equals(T x,T y){ return _comparer(x,y); } public int GetHashCode(T obj){ return _hash(obj); } } </code></pre> <p>Usage:</p> <pre><code>public void Foo{ public string Fizz{get;set;} public BarEnum Bar{get;set;} } public enum BarEnum {One,Two,Three} var lst=new List&lt;Foo&gt;(); lst.Distinct(new LambdaComparer&lt;Foo&gt;( (x1,x2)=&gt;x1.Fizz==x2.Fizz&amp;&amp; x1.Bar==x2.Bar)); </code></pre> <p>You can even wrap it around to avoid writing noisy <code>new LambdaComparer&lt;T&gt;(...)</code> thing:</p> <pre><code>public static class EnumerableExtensions{ public static IEnumerable&lt;T&gt; SmartDistinct&lt;T&gt; (this IEnumerable&lt;T&gt; lst, Func&lt;T, T, bool&gt; pred){ return lst.Distinct(new LambdaComparer&lt;T&gt;(pred)); } } </code></pre> <p>Usage:</p> <pre><code>lst.SmartDistinct((x1,x2)=&gt;x1.Fizz==x2.Fizz&amp;&amp;x1.Bar==x2.Bar); </code></pre> <p>NB: works reliably only for Linq2Objects</p>
    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. This table or related slice is empty.
    1. This table or related slice is empty.
    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