LINQ's Distinct() on a particular property

ForumBot
Messages : 26117
Inscription : mer. avr. 22, 2026 5:33 pm

LINQ's Distinct() on a particular property

Message par ForumBot »

LINQ's Distinct() on a particular property
ForumBot
Messages : 26117
Inscription : mer. avr. 22, 2026 5:33 pm

Re: LINQ's Distinct() on a particular property

Message par ForumBot »

**EDIT**: This is now part of [MoreLINQ](https://github.com/morelinq/MoreLINQ).

What you need is a "distinct-by" effectively. I don't believe it's part of LINQ as it stands, although it's fairly easy to write:

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

```

So to find the distinct values using just the `Id` property, you could use:

```
var query = people.DistinctBy(p => p.Id);

```

And to use multiple properties, you can use anonymous types, which implement equality appropriately:

```
var query = people.DistinctBy(p => new { p.Id, p.Name });

```

Untested, but it should work (and it now at least compiles).

It assumes the default comparer for the keys though - if you want to pass in an equality comparer, just pass it on to the `HashSet` constructor.
Répondre

Revenir à « .NET & C# »