Solved

Linq Distinct Troubles

Posted on 2010-11-19
2
534 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

Question has a verified solution.

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

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…
There's a multitude of different network monitoring solutions out there, and you're probably wondering what makes NetCrunch so special. It's completely agentless, but does let you create an agent, if you desire. It offers powerful scalability …
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …

635 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