Linq Distinct on a particular Property?

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; } } } 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.

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; } } } 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.

This is a good solution, if you assume that when there are multiple non-distinct values (like in his example) you're aiming to return the first one that you see in the enumeration. – mquander Jan 28 '09 at 21:20 Yes, that's what I was assuming based on the question. If he'd requested Person2 and Person3 it would be harder :) – Jon Skeet Jan 28 '09 at 21:23 I think you did an error... the IF statement should not have the!.

You want to yield when the element is added..right? – Daok Jan 28 '09 at 21:44 Yup, fixing thanks. – Jon Skeet Jan 28 '09 at 21:50 Thx for all your answer today Jon, I am searching the web with your solution and start to understand.

On this thread I have learn about Func. Thx – Daok Jan 28 '097 at 1:52.

Simple! You want to group them and pick a winner out of the group. List distinctPeople = allPeople .

GroupBy(p => p. PersonId) . Select(g => g.First()) .ToList(); If you want to define groups on multiple properties, here's how: List distinctPeople = allPeople .

GroupBy(p => new {p. PersonId, p. FavoriteColor} ) .

Select(g => g.First()) .ToList().

Thx for the example! +1 – Daok Jan 30 '09 at 16:18 Awesome, that is exactly what I was looking for - Thanks! – VoodooChild Nov 8 '10 at 17:08 Great answer, this makes it simple +1 – Nick yesterday.

You can do it (albeit not lightning-quickly) like so: people. Where(p =>!people. Any(q => (p!

= q && p. Id == q. Id))); That is, "select all people where there isn't another different person in the list with the same ID."

Mind you, in your example, that would just select person 3. I'm not sure how to tell which you want, out of the previous two.

You should be able to override Equals on person to actually do Equals on Person.id. This ought to result in the behavior you're after.

I realize this is a bit late... but I've written an article that explains how to extend the Distinct function so that you can do as follows: var people = new List(); people. Add(new Person(1, "a", "b")); people. Add(new Person(2, "c", "d")); people.

Add(new Person(1, "a", "b")); foreach (var person in people. Distinct(p => p. ID)) // do stuff with unique list here.

Here's the article: Extending LINQ - Specifying a Property in the Distinct Function.

Your article has an error, there should be a after Distinct: public static IEnumerable Distinct(this... Also it does not look like it will work (nicely) on more that one property i.e. A combination of first and last names. – row1 Mar 17 '10 at 10:01.

I am playing with Linq to learn about it but I can't figure out how to Distinct when I do not have a simple list (a simple list of integer is pretty easy to do, this is not the question). What if want to distinct a list of Object on ONE or MORE Properties of the object? Example: If an object is "Person", with Property "Id".

How can I get all Person and distinct them by the property Id of the object? How can I get just Person1 and Person3? Is that possible?

If it's not possible with Linq, what would be the best way to have a list of "Person" depending of some of its Properties in .

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions