Solved

Linq Distinct Troubles

Posted on 2010-11-19
2
514 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

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

757 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

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now