Solved

Linq Distinct Troubles

Posted on 2010-11-19
2
517 Views
Last Modified: 2013-11-11
I am tiring to select distinct values by a object property.  What am I doing wrong?  I am getting the errors:


Error      14      'System.Linq.IOrderedEnumerable<MB.Core.Record>' does not contain a definition for 'Distinct' and the best extension method overload 'System.Linq.ParallelEnumerable.Distinct<TSource>(System.Linq.ParallelQuery<TSource>, System.Collections.Generic.IEqualityComparer<TSource>)' has some invalid arguments      

and

Error      15      Instance argument: cannot convert from 'System.Linq.IOrderedEnumerable<MB.Core.Record>' to 'System.Linq.ParallelQuery<MB.Core.RecordLesson>'

Code Posted Below.

Thanks!

List<Record> allStudentRecords = RecordBuilder.GetAllRecordsByStudentId(CurrentStudent.StudentId).OrderByDescending(r => r.RecordLevel).Distinct(new DistinctLesson()).ToList<Record>();


    public class DistinctLesson : IEqualityComparer<RecordLesson>
    {
        public bool Equals(RecordLesson x, RecordLesson y)
        {
            return x.RecordLessonName.Equals(y.RecordLessonName);
        }

        public int GetHashCode(RecordLesson obj)
        {
            return obj.RecordLessonName.GetHashCode();
        }
    }

Open in new window

0
Comment
Question by:grogo212
2 Comments
 
LVL 22

Accepted Solution

by:
Snarf0001 earned 500 total points
ID: 34176866
What type does RecordBuilder.GetAllRecordsByStudentId return?

It appears from the code that it returns a Record object, but you're trying to pass it to an equality comparer that expects a RecordLesson object.
0
 

Author Comment

by:grogo212
ID: 34176896
Your Right! Silly mistake

Here's my modified code:
public class DistinctLesson : IEqualityComparer<Record>
    {
        public bool Equals(Record x, Record y)
        {
            return x.RecordLesson.RecordLessonName.Equals(y.RecordLesson.RecordLessonName);
        }

        public int GetHashCode(Record obj)
        {
            return obj.RecordLesson.RecordLessonName.GetHashCode();
        }
    }

Open in new window

0

Featured Post

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…

778 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question