-
Notifications
You must be signed in to change notification settings - Fork 4.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Feature Request: Overload for Distinct to receive a func. #27665
Comments
To be efficient, wouldn't it need two funcs, one for equality and one for hash code? |
what would the hash code do? i dont really know how efficient it would be. |
Distinct works by building a hash set (https://github.com/dotnet/corefx/blob/master/src/System.Linq/src/System/Linq/Set.cs) so that for each new item it sees, it can hash it and determine relatively quickly whether it's already seen that item. That requires a hash code, and that's why IEqualityComparer has both a comparison and hash code method. Without that, Distinct would end up being O(N^2), having to compare every new item individually against every previous item yielded to determine whether it was the same. So the overload would need to take two delegates, and the call site would end up being quite complicated. I don't think this is something we should add. If we wanted to do something, I'd prefer to see us add the equivalent of Comparer.Create for EqualityComparer, with EqualityComparer.Create taking the same two delegates. That could then be used with Distinct, but also elsewhere. |
I think the right way to improve this is to add With that and C# 7.0 tuples, you could write: DistinctBy(a => (a.Stuff, a.Stuff1)) This is simpler to use than the proposed overload of |
i like @svick idea alot, how hard would it be? |
There should by all kinds of I have seen some of them proposed in a scattered around way. I would be willing to make a ticket with a formal API proposal that centralizes all of the |
Or by merging the Select into the GroupBy:
But with performance opportunities that these equivalents do not have. |
Same situation with MaxBy.
|
any news on this? will it enter in any release? |
Edit: Actually it's the other way around |
@jnm2 Where did you see that? The documentation for
So, at least this overload does guarantee the order. (Though not all overloads have this note.) |
Oh no, I had them exactly backwards! GroupBy preserves ordering and Distinct does not. |
So, something like this? public static class Enumerable
{
public static IEnumerable<TSource> DistinctBy<TSource, TKey>(this IEnumerable<TSource> source, Func<TSource, TKey> keySelector);
} FWIW F# core has a similar method. |
This request is already covered by #27687. Closing this one. |
I noticed in the Distinct there is no overload for receiving a func, we always have to create a new lambdacomparer and put the func inside the comparer, is it possible to have that abstracted and use it like:
Distinct((a, b) => a.Stuff == b.Stuff && a.Stuff1 == b.Stuff1)
Does this even make sense?
If so i think i have a simple solution for this.
The text was updated successfully, but these errors were encountered: