I have two version of grouping by a list of items
List<m_addtlallowsetup> xlist_distincted = xlist_addtlallowsetups.DistinctBy(p => new { p.setupcode, p.allowcode }).OrderBy(y => y.setupcode).ThenBy(z => z.allowcode).ToList();
and groupby
List <m_addtlallowsetup> grouped = xlist_addtlallowsetups.GroupBy(p => new { p.setupcode, p.allowcode }).Select(grp => grp.First()).OrderBy(y => y.setupcode).ThenBy(z => z.allowcode).ToList();
these two seemed to me that they are just the same, but there's gotta be a layman's explanation of their difference, their performance and disadvantages
Let's review the
MoreLinqAPIs first, following is the code forDistinctBy:MoreLinq - DistinctBy
Source Code
Working
HashSet<T>internally it just checks the first match and returns the first element of TypeTmatching the Key, rest are all ignored, since Key is already added to the HashSetFunc<TSource, TKey> keySelectorEnumerable - GroupBy
(Source Code)
Working
LookUpdata structure to group all the data for a given KeySummary
MoreLinq - DistinctByachieves a small subset of whatEnumerable - GroupBycan achieve. In case your use case is specific, use the More Linq APIMoreLinq - DistinctBywould be faster, since unlikeEnumerable - GroupBy,DistinctBydoesn't first aggregate all data and then select first for each unique Key, MoreLinq API just ignores data beyond first recordMoreLinqis a better choice.This is a classic case in Linq, where more than one API can provide same result but we need to be wary of the cost factor, since
GroupByhere is designed for much wider task than what you are expecting fromDistinctBy