C# .NET Core linq Distinct

var distinctUsers = allUsers
    .GroupBy(x => x.UserId)
    .Select(x => x.First())
    .ToList();

4.13
8

                                    List<Person> distinctPeople = allPeople
  .GroupBy(p => new {p.PersonId, p.FavoriteColor} )
  .Select(g => g.First())
  .ToList();

4.13 (8 Votes)
0
4.33
6
Daniel V 170 points

                                    public static IEnumerable<TSource> DistinctBy<TSource, TKey>
    (this IEnumerable<TSource> source, Func<TSource, TKey> keySelector)
{
    HashSet<TKey> seenKeys = new HashSet<TKey>();
    foreach (TSource element in source)
    {
        if (seenKeys.Add(keySelector(element)))
        {
            yield return element;
        }
    }
}

4.33 (6 Votes)
0
Are there any code examples left?
Create a Free Account
Unlock the power of data and AI by diving into Python, ChatGPT, SQL, Power BI, and beyond.
Sign up
Develop soft skills on BrainApps
Complete the IQ Test
Relative searches
c# linq use distinct c# .net linq distinct .net linq distinct linq c# distinct select distinct record in asp net core linq .net c# distinct by multiple properties get distinct values using linq in c#.net c# list distinct by field system linq distinct by .net distinct comparer C# ensure unique method names c# linq select distinct int distinct c# entity framework c# distinct how to use distinct in linq query in c# LINQ Distinct Operation with Complex Type: how to make a model distinct in c# IEnumerable list distinct Distinct(). c# linq list.distinct list c# distinct linq linq filter distinct on list c# lambda distinct on property distinct c# how to use linq using distinct c# linq distinct list best way to handle distinct object c# linq distinct on a property Distinct .net core distinct by c3 c# distinct linq c# distinct query Distinct<TSource>(this IEnumerable<TSource> source, IEqualityComparer<TSource> comparer) Distinct in dot net core how to apply distinct on list in linq query disticnt linq c# list distinct disctinct method in linq list distinct c# Linq disnticnt distinct()where c# linq query distinct example c# how to use distinct in c# how to distinct list in c# linq using Distince C# using Distince C3 select distinct ienumerable c# distcint linq linq distinct by id examples .distinct() c# distinct in c# select unique from list c# base on property list .distinct c# distinct by c# linq linq distinct by property linq distinct by c#$ linq distinct distinct algorithm code c# linq filter unique distinc in LINQ distinct c# c# linq distint how to use distinct.GetEnumerator C# how to use distinct in linq distinct c# linq collection distinct c# distinct by a field c# Distinct linq c# distinct by field .distinct() linq c# list distinct by property how to select distinct in LINQ by properties linq distinct by field c# c# distinct list by object preperty c# .distinct c# linq unique c# distinct list Distinct() c# List unique linq c# select objects by distinct property c# distinct on a property distinct in .net core .distinct() c# distingc linq Distinct method in C# linq linq distinct example c# use distinct() on list distinct object by property c# distinct object property c# how to distinct all property in linq auto generate property distinct in linq all properties distinct in linq all properties c# distinct linq distinct property linq use distinct on property linq distinct sequence list c#$ distinct in linq linq from distinct .distinct c# linq distinct name linq list distinct c# linq distinct c# example distinct list c# use distinct in linq c# C# filter list on unique property of object distinct list of all object properties by property name c# get distinct value from list for particular prop Distinct c sharp distinc c# todistinc c# distinct in linq c# c# select distinct from list based on property enumerable.distinct in angularjs linq get object with distinct property from list select distinct property from list c# Dinstinct c# c# distinct comparer list linq distinct c# c# list distinct on one field list.distinct c# c# distinct by c# distinct list by properties c# distinct iequalitycomparer c# distinct by object property linq distinct list c# c# distinct by one property linq distinct object by property from distinct linq c# c# linq select distinct objects from list by property distinct based on property c# c# linq distinct list by prop distinct by property c# can distinct be run on an empoty list Linq c# string distinct c# linq distincty string distinct c# .net linq distinct get hash code .net linq distinct compare instances by hash code distinct linq c# C# .NET Core linq Distinct c# linq distinct linq .distinct linq distinct c# linq distinct c# distinct list of objects by property get distinct list c# linq distinct function in linque c# linq distinct by property distinct by c# c# linq select distinct by property c# distinct by property c# linq single and distinct() c# distinct comparer multiple properties
Made with love
This website uses cookies to make IQCode work for you. By using this site, you agree to our cookie policy

Welcome Back!

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign in
Recover lost password
Or log in with

Create a Free Account

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign up
Or sign up with
By signing up, you agree to the Terms and Conditions and Privacy Policy. You also agree to receive product-related marketing emails from IQCode, which you can unsubscribe from at any time.
Creating a new code example
Code snippet title
Source