Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
866 views
in Technique[技术] by (71.8m points)

c# - Linq: GroupBy vs Distinct

I've been trying to get a Linq query to return distinct values from a collection. I've found two ways to go about it; either use GroupBy or Distinct. I know that Distinct was made for the job but I have to implement IEquatable on the object.

I tried GroupBy and that worked just fine. I want to know if using Distinct vs GroupBy has a distinct performance advantage.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Distinct() will compare entire objects in collection (for reference types you need GetHashCode and Equals to be overridden). It will enumerate items and just add them to set. Simple and fast. Something like:

Set<TSource> set = new Set<TSource>(comparer);

foreach (TSource tSource in source)
{
     if (!set.Add(tSource))
          continue;

     yield return tSource;
}

GroupBy() allows you to group object by some key. In this case keys will be compared. It will need to execute key selector lambda for each item in collection. Also it will need to create grouping for each distinct key and add each item in collection to its group:

Func<TSource, TElement> elementSelector = x => x;

<TKey, TElement> lookup = new Lookup<TKey, TElement>(comparer);
foreach (TSource tSource in source)
{
     TKey key = keySelector(tSource);

     // simplified pseudo-code
     if (!lookup.Contains(key))
          lookup.Add(new Grouping(key)); 

     lookup[key].Add(elementSelector(tSource));
}

foreach(IGrouping<TKey, TElement> grouping in lookup)
    yield return grouping;

So, I think GroupBy() is not that fast as simple Distict().


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...