score:12

Accepted answer

for distinct() (and many other linq features) to work, the class being compared (barobject in your example) must implement implement equals() and gethashcode(), or alternatively provide a separate iequalitycomparer<t> as an argument to distinct().

many linq methods take advantage of gethashcode() for performance because internally they will use things like a set<t> to hold the unique items, which uses hashing for o(1) lookups. also, gethashcode() can quickly tell you if two objects may be equivalent and which ones are definitely not - as long as gethashcode() is properly implemented of course.

so you should make all your classes you intend to compare in linq implement equals() and gethashcode() for completeness, or create a separate iequalitycomparer<t> implementation.

score:-1

should be as simple as:

var foo = (from data in pivoteddata.asenumerable()
               select new
               {
                 group = data.field<string>("group_number"),
                 study = data.field<string>("study_name")
               }).distinct().select(x => new barobject {
                 group = x.group,
                 study = x.study
               });

score:1

try this:

var foo = (from data in pivoteddata.asenumerable().distinct()
                   select new barobject
                   {
                     group = data.field<string>("group_number"),
                     study = data.field<string>("study_name")
                   });

score:2

you want to use the other overload for distinct() that takes a comparer. you can then implement your own iequalitycomparer<barobject>.

score:3

looks like distinct can not compare your barobject objects. therefore it compares their references, which of course are all different from each other, even if they have the same contents.

so either you overwrite the equals method, or you supply a custom equalitycomparer to distinct. remember to overwrite gethashcode when you implement equals, otherwise it will produce strange results if you put your objects for example into a dictionary or hashtable as key (e.g. hashset<barobject>). it might be (don't know exactly) that distinct internally uses a hashset.

here is a collection of good practices for gethashcode.

score:4

you need to override equals and gethashcode for barobject because the equalitycomparer.default<barobject> is reference equality unless you have provided overrides of equals and gethashcode (this is what enumerable.distinct<barobject>(this ienumerable<barobject> source) uses). alternatively, you can pass in an iequalitycomparer<barobject> to enumerable.distinct<barobject>(this ienumerable<barobject>, iequalitycomparer<barobject>).

score:4

either do as dlev suggested or use:

var foo = (from data in pivoteddata.asenumerable()
               select new barobject
               {
                 group = data.field<string>("group_number"),
                 study = data.field<string>("study_name")
               }).groupby(x=>x.group).select(x=>x.firstordefault())

check this out for more info http://blog.jordanterrell.com/post/linq-distinct()-does-not-work-as-expected.aspx


Related Query

More Query from same tag