I believe this post explains your problem: blog.jordanterrell.com/post/LINQ-Distinct()-does-not-work-as-expected.aspx.
This worked great. Thanks. – Tim Almond Dec 15 '10 at 10:41.
Try an IQualityComparer public class MyObjEqualityComparer : IEqualityComparer { public bool Equals(MyObj x, MyObj y) { return x.Category. Equals(y. Category) && x.Country.
Equals(y. Country); } public int GetHashCode(MyObj obj) { return obj.GetHashCode(); } } then use here var comparer = new MyObjEqualityComparer(); myObjs. Where(m => m.
SomeProperty == "whatever"). Distinct(comparer).
For explanation, take a look at other answers. I'm just providing one way to handle this issue. You might like this: public class LambdaComparer:IEqualityComparer{ private readonly Func _comparer; private readonly Func _hash; public LambdaComparer(Func comparer): this(comparer,o=>0) {} public LambdaComparer(Func comparer,Func 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); } } Usage: public void Foo{ public string Fizz{get;set;} public BarEnum Bar{get;set;} } public enum BarEnum {One,Two,Three} var lst=new List(); lst.
Distinct(new LambdaComparer( (x1,x2)=>x1. Fizz==x2. Fizz&& x1.
Bar==x2. Bar)); You can even wrap it around to avoid writing noisy new LambdaComparer(...) thing: public static class EnumerableExtensions{ public static IEnumerable SmartDistinct (this IEnumerable lst, Func pred){ return lst. Distinct(new LambdaComparer(pred)); } } Usage: lst.
SmartDistinct((x1,x2)=>x1. Fizz==x2. Fizz&&x1.
Bar==x2. Bar); NB: works reliably only for Linq2Objects.
Until recently, I was using a Distinct in LINQ to select a distinct category (an enum) from a table. This was working fine. I now need to have it distinct on a class containing a category and country (both enums).
The Distinct isn't working now. What am I doing wrong?
I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.