You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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:
IEnumerable<IEnumerable<T>> 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.
Reacted by Petr Onderka, Joseph Musser, Sam and Jyrki Vesterinen
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.
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.
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.
@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)
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.
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.
SplitToChunks
AsChunked
I do not know of another name that I would find acceptable.
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.
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.
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.
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.
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.
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.
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.
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.
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
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.
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.
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.
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))
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
Activity
omariom commentedon Jun 24, 2015
👍 As I had to implement many of ..By methods myself.
Only suggestion is to add them as overloads to existing methods.
eatdrinksleepcode commentedon Jun 24, 2015
These seem like two distinct proposals that should be two distinct issues.
I agree with both proposals.
Clockwork-Muse commentedon Jun 26, 2015
Distinct,Except,Intersect, andUnionall have an overload that take anIEqualityComparer<T>(your description ofIntersectis also worryingly off...).MaxandMinalso have predicate overloads likeOrderBy. 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 (
Distinctclaims 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
GroupByto 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 callToLookupor something.svick commentedon Jun 26, 2015
@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 implementsIEqualityComparer<T>(though you can use a library to help you with that).MinByandMaxByare different thanMinandMax. 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, whilepeople.MinBy(p => p.Id)returns the whole person.Partitioneris 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 andPartitionerdoesn't really help you with that. Actually, it doesn't even fit:Partitioner.GetPartitions()takespartitionCount, 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 (Selectto get the index,GroupBy,Selectagain to get rid of the index) and I think that's too complicated.Clockwork-Muse commentedon Jun 26, 2015
@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/Mincan'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
GroupByI was thinking of something a little more hacky than that, but that's probably what you'd need for PLINQ)omariom commentedon Jun 26, 2015
I missed that. Suffix "By" is needed.
GSPP commentedon Jun 26, 2015
@Clockwork-Muse IntersectBy should be fixed now.
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.
Clockwork-Muse commentedon Jul 9, 2015
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/MaxByended up acting more likeOrderByet al - that is,nullis a valid, and (depending on key type), the lowest value. This is in direct contrast toMax/Mincurrently, which only returnnullif 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.
GSPP commentedon Jul 9, 2015
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.I do not know of another name that I would find acceptable.
Clockwork-Muse commentedon Jul 9, 2015
@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....?
JonHanna commentedon Jul 9, 2015
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>andIEnumerable<T>? I've seen a few implementations of some of these that only support the latter, though it shouldn't be hard to have:And so on. Therefore
IQueryable<T>would retain parity withIEumerable<T>without having to fall-back to in-memory operations and without forcing providers to upgrade to match by supportingMaxByand 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 thatreturn 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 forMaxBy, so much the better).An alternative form of the above would have:
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.
GSPP commentedon Jul 9, 2015
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.
JonHanna commentedon Jul 10, 2015
In that case, I think the additions would be more trouble than it's worth.
Taking
MaxByas 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 withIEnumerable<T>write it themselves or use a third-party implementation.Approach people will take if
MaxByis engineered to work with current query providers:Use
.MaxBy(keySelector).Approach people will take if
MaxByis 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.JohanLarsson commentedon Jul 10, 2015
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.
Add Linq methods keyed on expressions: MaxBy, DistinctBy, etc.
21 remaining items
GSPP commentedon Jan 20, 2016
Another thought: This design of MaxBy does not support multiple criteria, right? Sorting does. Maybe we should have
MayBy(Func, Func, ...)overloads and aMayBy(Func[])overload. Or, adopt a similar design toThenByalthough that causes allocations. Not sure what the right approach is.In my private utility library I have chosen the overload approach.
JonHanna commentedon Jan 20, 2016
The main thing that would make a well-optimised
MaxByfaster thanOrderBy(…).First()is the fact thatMaxBydoesn't support multiple criteria so paths related to the possibility ofThenBy(…)won't be there. That would be a small O(n) addition to the time thatMaxBy()could get rid of. Stop that being a feature ofMaxByand you lose that part of the optimisation that offeringMaxByallows. There'd also be a small constant overhead in the extra method constructing theIOrderedEnumerable-implementing object that then hasFirst()called on it.GSPP commentedon Jan 20, 2016
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
EnumerableSorterseems to confirm this.QuickSelectlooks like it should be quite fast but for a cheapCompareKeysit still does more than a naiveMaxBywould do.MayBy implementations should be specialized to the number of criteria for small numbers of criteria (1-3?). That potential disadvantage can be solved.
JonHanna commentedon Jan 20, 2016
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.
JonHanna commentedon Jan 20, 2016
(That extra overhead is proportional to the number of potential tiebreak cases introduced by ThenBy steps, which multi key MaxBy would add to MaxBy).
VSadov commentedon Nov 19, 2016
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
It is unclear if the benefits of an additional way to do the same thing would be attractive enough to expand the library.
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.
svick commentedon Nov 19, 2016
@VSadov
I think that at least
MinBy/MaxByand chunking are fairly common.You didn't address
MinByandMaxByat all. What is the reason for rejecting those? Is it becauseOrderBy(…).First()/.Last()is now efficient? If that is the reason, my concern is thatOrderBy(…).First()looks slow, whileMinBy()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.
jnm2 commentedon Nov 19, 2016
@VSadov I completely agree with @svick, came to argue the same. IComparer does not cut it.
GSPP commentedon Nov 27, 2016
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:
Or:
The way to query many rows by some key from an RDBMS is:
Trivial.
Thaina commentedon Feb 6, 2019
I think this really should support in core library, it really common
Actually the linq
MinandMaxitself is the one that was uncommon. And we can replicate its behaviour ofMinandMaxbyMinByandMaxByIt a little more verbose with this specific task but
MinByis more versatile to handle many more situationAsChunkedit 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 thanSequenceEqualPlease reopen this