Click here to Skip to main content
15,916,019 members
Home / Discussions / C#
   

C#

 
AnswerRe: c# ListView header text word wrap option Pin
Pete O'Hanlon4-Jul-14 1:48
mvePete O'Hanlon4-Jul-14 1:48 
GeneralRe: c# ListView header text word wrap option Pin
Galym4-Jul-14 17:39
professionalGalym4-Jul-14 17:39 
Questionhow to scan file on virustotal Pin
Member 105796733-Jul-14 17:42
Member 105796733-Jul-14 17:42 
AnswerRe: how to scan file on virustotal Pin
Richard MacCutchan4-Jul-14 0:49
mveRichard MacCutchan4-Jul-14 0:49 
Questionaccessing public static member of one class in another class Pin
KRISHNARAYALU3-Jul-14 11:59
KRISHNARAYALU3-Jul-14 11:59 
AnswerRe: accessing public static member of one class in another class Pin
Matt T Heffron3-Jul-14 12:32
professionalMatt T Heffron3-Jul-14 12:32 
GeneralRe: accessing public static member of one class in another class Pin
KRISHNARAYALU4-Jul-14 0:48
KRISHNARAYALU4-Jul-14 0:48 
GeneralRe: accessing public static member of one class in another class Pin
Pete O'Hanlon4-Jul-14 1:40
mvePete O'Hanlon4-Jul-14 1:40 
AnswerRe: accessing public static member of one class in another class Pin
Dave Kreskowiak3-Jul-14 18:05
mveDave Kreskowiak3-Jul-14 18:05 
AnswerRe: accessing public static member of one class in another class Pin
Richard MacCutchan4-Jul-14 0:48
mveRichard MacCutchan4-Jul-14 0:48 
Questionmoving slideshow Pin
Member 109229203-Jul-14 8:36
Member 109229203-Jul-14 8:36 
AnswerRe: moving slideshow Pin
User 48350473-Jul-14 9:22
User 48350473-Jul-14 9:22 
QuestionMongo DB Projection from C# Pin
Ennis Ray Lynch, Jr.3-Jul-14 8:22
Ennis Ray Lynch, Jr.3-Jul-14 8:22 
AnswerRe: Mongo DB Projection from C# Pin
Richard Deeming3-Jul-14 8:47
mveRichard Deeming3-Jul-14 8:47 
GeneralRe: Mongo DB Projection from C# Pin
Ennis Ray Lynch, Jr.3-Jul-14 9:20
Ennis Ray Lynch, Jr.3-Jul-14 9:20 
QuestionInternet Cafe Management System Pin
Abdul-Qaadir2-Jul-14 10:14
Abdul-Qaadir2-Jul-14 10:14 
AnswerRe: Internet Cafe Management System Pin
Eddy Vluggen2-Jul-14 12:39
professionalEddy Vluggen2-Jul-14 12:39 
QuestionCannot convert lambda expression to type 'System.Collections.Generic.IEqualityComparer' because it is not a delegate type Pin
caguas2-Jul-14 7:04
caguas2-Jul-14 7:04 
AnswerRe: Cannot convert lambda expression to type 'System.Collections.Generic.IEqualityComparer' because it is not a delegate type Pin
Richard Deeming2-Jul-14 9:42
mveRichard Deeming2-Jul-14 9:42 
As you've found, the built-in Distinct extension method[^] doesn't provide an overload which compares objects by a property. You'll need to roll your own:
C#
using System;
using System.Collections.Generic;

public static class SetExtensions
{
    private static IEnumerable<TSource> DistinctByIterator<TSource, TKey>(
        IEnumerable<TSource> source, 
        Func<TSource, TKey> keySelector, 
        IEqualityComparer<TKey> keyComparer)
    {
        var knownKeys = new HashSet<TKey>(keyComparer);
        try
        {
            foreach (var item in source)
            {
                if (knownKeys.Add(keySelector(item)))
                {
                    yield return item;
                }
            }
        }
        finally
        {
            // HACK: Avoid iterator memory leak described at:
            // http://msmvps.com/blogs/jon_skeet/archive/2011/01/18/gotcha-around-iterator-blocks.aspx
            knownKeys = null;
        }
    }
    
    public static IEnumerable<TSource> DistinctBy<TSource, TKey>(
        this IEnumerable<TSource> source, 
        Func<TSource, TKey> keySelector, 
        IEqualityComparer<TKey> keyComparer = null)
    {
        if (source == null) throw new ArgumentNullException("source");
        if (keySelector == null) throw new ArgumentNullException("keySelector");
        return DistinctByIterator(source, keySelector, keyComparer);
    }
}


With that in place, you can update your query to use the new method:
C#
var topRatedProducts = (from product in _container.Products.Include("ProductReviews")
                        orderby product.ProductReviews.Average((x) => x.StarRate) descending
                        select product).DistinctBy(y => y.ProductName);




"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer


GeneralRe: Cannot convert lambda expression to type 'System.Collections.Generic.IEqualityComparer' because it is not a delegate type Pin
caguas3-Jul-14 6:46
caguas3-Jul-14 6:46 
QuestionHow to solve (Use of unassigned local variable) Pin
Jassim Rahma2-Jul-14 0:25
Jassim Rahma2-Jul-14 0:25 
AnswerRe: How to solve (Use of unassigned local variable) Pin
Pete O'Hanlon2-Jul-14 0:43
mvePete O'Hanlon2-Jul-14 0:43 
GeneralRe: How to solve (Use of unassigned local variable) Pin
Jassim Rahma2-Jul-14 0:54
Jassim Rahma2-Jul-14 0:54 
AnswerRe: How to solve (Use of unassigned local variable) Pin
Shameel2-Jul-14 1:01
professionalShameel2-Jul-14 1:01 
GeneralRe: How to solve (Use of unassigned local variable) Pin
Pete O'Hanlon2-Jul-14 1:11
mvePete O'Hanlon2-Jul-14 1:11 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.