Solved

Linq Distinct Troubles

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

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

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
How to repeat the data 4 30
PowerShell: ForEach ($y in $x.licenses | where {....} 3 55
WPF Select Items 3 27
Visual Studio 2015 auto inserted code 12 71
This document covers how to connect to SQL Server and browse its contents.  It is meant for those new to Visual Studio and/or working with Microsoft SQL Server.  It is not a guide to building SQL Server database connections in your code.  This is mo…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

726 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