Skip to content

Add commonly required Enumerable methods (DistinctBy, ExceptBy, AsChunked, ...) #14753

Closed

Description

@GSPP

We have Distinct, Except, Intersect, Union, etc. Often it is required, though, to base the set operation on some key. OrderBy can do that for example.

I request that the following methods be added:

  • DistinctBy (pick the first element deterministically for each key)
  • ExceptBy (return all items of the first sequence that do not have a matching key in the second)
  • IntersectBy (return the first item in a.Concat(b) for each unique key where the key appears in both collections)
  • UnionBy (return the first item in a.Concat(b) for each unique key)
  • MaxBy/MinBy

The set operations need to deal with duplicate keys in a sensible way. I have added to that list my idea for how to do that in sane and well-defined way.


Another commonly required helper method is one that splits a sequence into chunks of a fixed size:

static IEnumerable<IEnumerable<T>> AsChunked<T>(this IEnumerable<T> sequence, int chunkSize)

This is useful for many things, for example:

  • Split a list into pages
  • Make AsParallel operate on coarser items by feeding it chunks
  • If you want to save a huge stream of items to files you can use this to split the sequence into one chunk per file
  • Request rows from the database in chunks of 1000 IDs

This requirement comes up on Stack Overflow a lot and there is no built-in way to do it.

The chunks should be drawn lazily. In fact the items of each chunk should be lazy as well. This allows for very big chunks such as 1 billion items per chunk.

Maybe we need two variants:

  1. IEnumerable<IEnumerable<T>> AsChunkedLazy
  2. IEnumerable<T[]> AsChunkedEager

The second one would be easier to handle for consumers. The first one requires more disciplined use because each chunk must be enumerated at most once and the chunks must be enumerated in order. This is required for an efficient streaming operation.

Activity

  1. omariom commented on Jun 24, 2015

    @omariom
    Contributor

    👍 As I had to implement many of ..By methods myself.
    Only suggestion is to add them as overloads to existing methods.

  2. eatdrinksleepcode commented on Jun 24, 2015

    @eatdrinksleepcode

    These seem like two distinct proposals that should be two distinct issues.

    I agree with both proposals.

    On Jun 24, 2015, at 8:02 AM, GSPP [email protected] wrote:

    We have Distinct, Except, Intersect, Union, etc. Often it is required, though, to base the set operation on some key. OrderBy can do that for example.

    I request that the following methods be added:

    DistinctBy (pick the first element deterministically for each key)
    ExceptBy (return all items of the first sequence that do not have a matching key in the second)
    IntersectBy (return all items in both sequences that have at least one matching key)
    UnionBy (return the first item in a.Concat(b) for each unique key)
    MaxBy/MinBy
    The set operations need to deal with duplicate keys in a sensible way. I have added to that list my idea for how to do that in sane and well-defined way.

    Another commonly required helper method is one that splits a sequence into chunks of a fixed size:

    static IEnumerable<IEnumerable> AsChunked(this IEnumerable sequence, int chunkSize)
    This is useful for many things, for example:

    Split a list into pages
    Make AsParallel operate on coarser items by feeding it chunks
    If you want to save a huge stream of items to files you can use this to split the sequence into one chunk per file
    Request rows from the database in chunks of 1000 IDs
    This requirement comes up on Stack Overflow a lot and there is no built-in way to do it.

    The chunks should be drawn lazily. In fact the items of each chunk should be lazy as well. This allows for very big chunks such as 1 billion items per chunk.

    Maybe we need two variants:

    IEnumerable<IEnumerable> AsChunkedLazy
    IEnumerable<T[]> AsChunkedEager
    The second one would be easier to handle for consumers. The first one requires more disciplined use because each chunk must be enumerated at most once and the chunks must be enumerated in order. This is required for an efficient streaming operation.

    —
    Reply to this email directly or view it on GitHub.

  3. Clockwork-Muse commented on Jun 26, 2015

    @Clockwork-Muse
    Contributor

    Distinct, Except, Intersect, and Union all have an overload that take an IEqualityComparer<T> (your description of Intersect is also worryingly off...). Max and Min also have predicate overloads like OrderBy. How is that part of your proposal different?
    By definition, a "set" operation deals with duplicate keys by removing them. Vanilla LINQ (AFAICT) does this by choosing the first "unique" element (Distinct claims to be non-deterministic in documentation, but the current implementation behaves otherwise. The others either don't mention it or explicitly state the first one will be returned). PLINQ currently behaves non-deterministically even for ordered sources - I have an issue out for this, but don't know if it's considered a bug officially.

    If you're chunking, you need to/should implement the Partitioner<TSource> abstract class, which would manage some of the overhead for you. PLINQ normally assigns enumerables one internally, although you can create and feed it one of your own. The built-in enumerable ones don't provide pre-set chunk sizes, most likely because the runtime would have to count them (PLINQ tends to load-balance things taken, so...).
    You could probably use GroupBy to return you (small) chunked groupings - this would be pretty trivial in regular sequential LINQ (and more difficult in PLINQ, because you don't have control over thread interleaving). Or call ToLookup or something.

  4. svick commented on Jun 26, 2015

    @svick
    Member

    @Clockwork-Muse

    I believe the set operations are for convenience, but it's a significant convenience. DistinctBy(x => x.Id) is much simpler than writing a class that implements IEqualityComparer<T> (though you can use a library to help you with that).

    MinBy and MaxBy are different than Min and Max. The former return the value, the latter the whole item that contains the value. So e.g. people.Min(p => p.Id) returns the id, while people.MinBy(p => p.Id) returns the whole person.

    Partitioner is about splitting a collection into chunks for parallel processing. But I believe just splitting a collection into chunks of fixed size for other purposes is common enough and Partitioner doesn't really help you with that. Actually, it doesn't even fit: Partitioner.GetPartitions() takes partitionCount, while for chunking you want to set the size of a chunk, not their number.

    And you can use GroupBy() for this purpose, but it requires three method calls (Select to get the index, GroupBy, Select again to get rid of the index) and I think that's too complicated.

  5. Clockwork-Muse commented on Jun 26, 2015

    @Clockwork-Muse
    Contributor

    @svick - Okay. Hopefully they've implemented a reasonable hash code for their IEquatable (assuming it's a custom type). Might be simplest to wrap it in a delegating comparer to feed the IDictionary in the current implementation.

    @omariom - in such a case the set operations could be overloaded, but Max/Min can't be due to conflicts; what would the minimum of a type keyed by Id look like vs getting the minimum Id?

    You're right about the use of "Chunk" vs "Partition", although we'd almost certainly want a custom partitioner for such a type too....
    (For GroupBy I was thinking of something a little more hacky than that, but that's probably what you'd need for PLINQ)

  6. omariom commented on Jun 26, 2015

    @omariom
    Contributor

    in such a case the set operations could be overloaded, but Max / Min can't be due

    I missed that. Suffix "By" is needed.

  7. GSPP commented on Jun 26, 2015

    @GSPP
    Author

    @Clockwork-Muse IntersectBy should be fixed now.

    Distinct claims to be non-deterministic in documentation

    Yes, it returns the first item. For compat reasons this can never be changed now. It should simply be documented. I hereby request that as well.

    GroupBy instead of chunking is super tedious, slow, non-order-preserving (at least without additional work) and non-streaming.

  8. Clockwork-Muse commented on Jul 9, 2015

    @Clockwork-Muse
    Contributor

    I've done (At least some) tests and stubbed out PLINQ's side. What have I missed?

    From a behavior standpoint, the set operations can actually be implemented via a delegated equality comparer, that you could then use like return Distinct(source, new DelegatedEqualityComparer(predicate)). I used it to verify the tests for the new feature, but don't know all the performance ramifications of this.

    MinBy/MaxBy ended up acting more like OrderBy et al - that is, null is a valid, and (depending on key type), the lowest value. This is in direct contrast to Max/Min currently, which only return null if the collection is empty.

    I've stubbed out only one, completely lazy, version of Chunk, because I didn't know what the final direction on that was. Note that, when unordered, the behavior will likely be quite strange; at the moment the tests neither guarantee the number of chunks, nor the minimum number of elements in each chunk. The ordered version is expected to be able to guarantee both, but will be more expensive.... I can tighten that down, if necessary.

    I'll continue to update the tests as I get related PRs in (I have some other, ongoing work). If I run out of other stuff to do, I might try to implement this stuff.

  9. GSPP commented on Jul 9, 2015

    @GSPP
    Author

    I would object to the name Chunk. If someone asked you "What does a method named Chunk do?" you couldn't answer. Maybe you'd guess "it returns a chunk...?". The name is not descriptive enough. Here are some proposals in preference order.

    1. SplitToChunks
    2. AsChunked

    I do not know of another name that I would find acceptable.

  10. Clockwork-Muse commented on Jul 9, 2015

    @Clockwork-Muse
    Contributor

    @GSPP - I don't care too much about the names, so it can/will certainly be changed. I'd like to wait until I have a more definite direction....?

  11. JonHanna commented on Jul 9, 2015

    @JonHanna
    Contributor

    If these are to be added (and I'm not sure whether they should be) can we make it a requirement that they are supported for both IQueryable<T> and IEnumerable<T>? I've seen a few implementations of some of these that only support the latter, though it shouldn't be hard to have:

    public static TSource MaxBy<TSource, TKey>(this IQueryable<TSouce> source, Expression<Func<TSource, TKey>> keySelector)
    {
      if (source == null) throw Error.ArgumentNull("source");
      if (keySelector == null) throw Error.ArgumentNull("keySelector");
      var sorted = source.OrderByDescending(keySelector);
      return default(TSource) == null ? sorted.FirstOrDefault() : sorted.First();
    }
    

    And so on. Therefore IQueryable<T> would retain parity with IEumerable<T> without having to fall-back to in-memory operations and without forcing providers to upgrade to match by supporting MaxBy and so on directly. (If someone could think of a way of allowing direct-support in providers, without forcing it, and without depending upon catching the exception that return source.Provider.Execute<TSource>(Expression.Call(null, ((MethodInfo)MethodBase.GetCurrentMethod()).MakeGenericMethod(new Type[]{typeof(TSource), typeof(TKey)}), new Expression[]{source.Expression, Expression.Quote(keySelector)})); would throw if a provider didn't cater for MaxBy, so much the better).

    An alternative form of the above would have:

    public static TSource MaxBy<TSource, TKey>(this IQueryable<TSouce> source, Expression<Func<TSource, TKey>> keySelector)
    {
      if (source == null) throw Error.ArgumentNull("source");
      if (source.Provider is EnumerableQuery<TSource>)
        return source.AsEnumerable().MaxBy(keySelector.Compile());
      if (keySelector == null) throw Error.ArgumentNull("keySelector");
      var sorted = source.OrderByDescending(keySelector);
      return default(TSource) == null ? sorted.FirstOrDefault() : sorted.First();
    }
    

    This avoids taking the more expensive approach for linq-to-objects of sorting in memory before taking the first if an in-memory collection is accessed as a queryable, at the cost of adding an extra check.

  12. GSPP commented on Jul 9, 2015

    @GSPP
    Author

    I think all IQueryable expressions should go through the same process. No fallback methods. Represent queries full-fidelity and let the provider handle it. EF can be easily extended to implement the solution that you mentioned.

    Query providers should throw exceptions for anything they do not understand.

  13. JonHanna commented on Jul 10, 2015

    @JonHanna
    Contributor

    In that case, I think the additions would be more trouble than it's worth.

    Taking MaxBy as one instance.

    Approach people take in the current situation:

    Either use .OrderByDescending(keySelector).First() (barely a line of code) or if they want the performance gain a tighter approach offers with IEnumerable<T> write it themselves or use a third-party implementation.

    Approach people will take if MaxBy is engineered to work with current query providers:

    Use .MaxBy(keySelector).

    Approach people will take if MaxBy is provided in the same way as other queryable methods:

    Use MaxBy. Get error from provider. Go to stackoverflow.com and ask question about the error. Have question closed as duplicate. Read answers from duplicate question. Hopefully use .OrderByDescending(keySelector).First() but possibly use .AsEnumerable().MaxBy(keySelector). Or just use .AsEnumerable().MaxBy(keySelector) because .AsEnumerable() is the magic fixer. (Or gods help them, .ToList().MaxBy(keySelector) because educational examples of Linq overuse .ToList() and that's what people are learning from.)

    (Of course, we could provide it just on enumerables, but then people would likely use it and effectively be doing AsEnumreable().MaxBy(keySelector)).

    At the same time, I doubt there would be many implementations that wouldn't result in the same behaviour as if .OrderByDescending(keySelector).First() had been used, and if there were providers who could offer a much improved implementation could they not just do so for that pattern in the expression?

    These are meant to be convenience methods. A method that throws on some providers and not others isn't very convenient. As it is lack of support for Last() and some other methods with some providers is a nuisance.

  14. JohanLarsson commented on Jul 10, 2015

    @JohanLarsson

    What about an 'Extended LINQ' nuget package out-of-band?
    I like this suggestion, think MinBy, MaxBy & DistinctBy are very useful and should not be rejected as code bloat.
    Also, Rx & F# has them.

  15. 21 remaining items

  16. GSPP commented on Jan 20, 2016

    @GSPP
    Author

    Another thought: This design of MaxBy does not support multiple criteria, right? Sorting does. Maybe we should have MayBy(Func, Func, ...) overloads and a MayBy(Func[]) overload. Or, adopt a similar design to ThenBy although that causes allocations. Not sure what the right approach is.

    In my private utility library I have chosen the overload approach.

  17. JonHanna commented on Jan 20, 2016

    @JonHanna
    Contributor

    The main thing that would make a well-optimised MaxBy faster than OrderBy(…).First() is the fact that MaxBy doesn't support multiple criteria so paths related to the possibility of ThenBy(…) won't be there. That would be a small O(n) addition to the time that MaxBy() could get rid of. Stop that being a feature of MaxBy and you lose that part of the optimisation that offering MaxBy allows. There'd also be a small constant overhead in the extra method constructing the IOrderedEnumerable-implementing object that then has First() called on it.

  18. GSPP commented on Jan 20, 2016

    @GSPP
    Author

    I imagine that producing the first element of a sort is still quite expensive despite being O(1). In fact the whole sequence must be materialized which is O(N) space whereas MaxBy always is O(1) space. Looking at EnumerableSorter seems to confirm this. QuickSelect looks like it should be quite fast but for a cheap CompareKeys it still does more than a naive MaxBy would do.

    MayBy implementations should be specialized to the number of criteria for small numbers of criteria (1-3?). That potential disadvantage can be solved.

  19. JonHanna commented on Jan 20, 2016

    @JonHanna
    Contributor

    QuickSelect is only used for ElementAt/OrDefault. First/OrDefault and Last/OrDefault use a Θ(n) worse-case path that is O(1) in space. They're essentially MaxBy and MinBy. The only extra overhead is creating the comparer used.

  20. JonHanna commented on Jan 20, 2016

    @JonHanna
    Contributor

    (That extra overhead is proportional to the number of potential tiebreak cases introduced by ThenBy steps, which multi key MaxBy would add to MaxBy).

  21. VSadov commented on Nov 19, 2016

    @VSadov
    Member

    It is the goal of Linq to support all possible scenarios. Any additional API introduces costs of ongoing support, and additional size.

    As I see there are two parts in this proposal

    1. XxxxBy methods. - It seems like most of that functionality is already expressible via custom comparers.
      It is unclear if the benefits of an additional way to do the same thing would be attractive enough to expand the library.
    2. An API to split a sequence in to subsequences of some predefined size.
      Again - Is that a common scenario to put in System.Linq?
      Why the same size? Should it fail if minimum number of items is not available or just return shorter subsequence? The "lazy" part is not very clear either. Can it work at all if the source is not "seekable" like an array or a list?

    "Chunking" seems to be a special case scenario, that would not come up very often and when it would come up, a simple solution may not be sufficient.

  22. svick commented on Nov 19, 2016

    @svick
    Member

    @VSadov

    I think that at least MinBy/MaxBy and chunking are fairly common.


    You didn't address MinBy and MaxBy at all. What is the reason for rejecting those? Is it because OrderBy(…).First()/.Last() is now efficient? If that is the reason, my concern is that OrderBy(…).First() looks slow, while MinBy() looks like it's optimized exactly for this case. That increases chances that people will use this instead of writing their own code to do it and also makes the performance of the code easier to understand.


    I don't think chunking is a special case. I think APIs (especially web APIs) that accept a limited number of inputs are fairly common1 and chunking is the natural solution to that. I'm not sure what is the right solution regarding laziness, but even the simple solution returning lazy enumerable of eager enumerables should be enough for the common uses.

    1 Specifically, I encountered this when making a SQL Server query that exceeded some limit on the query size. And also when using the MediaWiki API to request information about multiple articles based on their ID, where the limit is 50 articles per query.

  23. jnm2 commented on Nov 19, 2016

    @jnm2
    Contributor

    @VSadov I completely agree with @svick, came to argue the same. IComparer does not cut it.

  24. GSPP commented on Nov 27, 2016

    @GSPP
    Author

    Chunking is so common. All business apps that I have worked on needed this. Processing data in batches is often required. Many Stack Overflow questions have a simple answer provided a chunking primitive is available.

    I often see ad hoc chunking code and it's nasty. Most of the time, batching should look like:

    foreach (var batch in items.AsChunked(1000)) ...
    

    Or:

    items
    .AsChunked(1000)
    .Select(batch => ...)
    

    The way to query many rows by some key from an RDBMS is:

    var keys = ...; //huge
    
    keys
    .AsChunked(1000)
    .SelectMany(keyBatch => Load(keyBatch))
    

    Trivial.

  25. Thaina commented on Feb 6, 2019

    @Thaina

    I think this really should support in core library, it really common

    Actually the linq Min and Max itself is the one that was uncommon. And we can replicate its behaviour of Min and Max by MinBy and MaxBy

    var minValue = items.Min((item) => item.value);
    // same
    var minValue = items.MinBy((item) => item.value).value;

    It a little more verbose with this specific task but MinBy is more versatile to handle many more situation

    AsChunked it also too common to draw it from nuget. I just want to use this only function and it need to come with whole MoreLinq library. I think it would be of used even more than SequenceEqual

    Please reopen this

  26. transferred this issue fromdotnet/corefxon Jan 31, 2020
  27. added this to the 2.0.0 milestone on Jan 31, 2020
  28. ghost locked as resolved and limited conversation to collaborators on Jan 5, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    api-needs-workarea-System.Linq

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions