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
{{ message }}
Repository navigation
[API Proposal]: Enumerable.Order and OrderDescending #67194
Linq provides OrderBy and OrderByDescending which implies that I'm sorting based on some part of the T. Very often I find myself wanting to just sort by the T itself.
If we add this, I think the name should just be Order rather than Ordered.
Reacted by Ian Mercer (Willow), Arpad Barta, Immo Landwerth, Joseph Musser, Jimmy Cushnie, Frederic Morel, Nick Heath, Iustin, Roman Marusyk, Christian Nagel and 1 more
Could they be called Order and OrderDescending instead, dropping the past tense "ed" from the names?
I agree, with a note that "Ordered" implies that the method will return data in some form, but does not communicate that there will be a substantial cost disproportional to size of the input. "Order" is suggesting that there is work behind constructing the result.
Could they be called Order and OrderDescending instead, dropping the past tense "ed" from the names?
I agree, with a note that "Ordered" implies that the method will return data in some form, but does not communicate that there will be a substantial cost disproportional to size of the input. "Order" is suggesting that there is work behind constructing the result.
Adding up, I feel explicitly defining the critetia (OrderAscending and OrderDescending) feels more natural.
changed the title [-][API Proposal]: Enumerable.Ordered and OrderedDescending[/-][+][API Proposal]: Enumerable.Order and OrderDescending[/+]on Mar 26, 2022
Since this keeps coming up, we recommend first doing an analysis of Enumerable to see if we can do something that is consistent and not explosive, like making selector functions nullable (and defaulted to null).
We did the analysis on the call. Losing generic inference makes that a worse approach.
We removed the generic constraint both for consistency, and IEnumerable<int?>
Should also apply to Queryable
namespaceSystem.Linq{publicpartialclassEnumerable{publicstaticIOrderedEnumerable<T>Order<T>(thisIEnumerable<T>source);publicstaticIOrderedEnumerable<T>Order<T>(thisIEnumerable<T>source,IComparer<T>comparer);publicstaticIOrderedEnumerable<T>OrderDescending<T>(thisIEnumerable<T>source);publicstaticIOrderedEnumerable<T>OrderDescending<T>(thisIEnumerable<T>source,IComparer<T>comparer);}publicpartialclassQueryable{// Or whatever's correct.[DynamicDependency("Order`1",typeof(Enumerable))]publicstaticIOrderedQueryable<T>Order<T>(thisIQueryable<T>source);[DynamicDependency("Order`1",typeof(Enumerable))]publicstaticIOrderedQueryable<T>Order<T>(thisIQueryable<T>source,IComparer<T>comparer);[DynamicDependency("OrderDescending`1",typeof(Enumerable))]publicstaticIOrderedQueryable<T>OrderDescending<T>(thisIQueryable<T>source);[DynamicDependency("OrderDescending`1",typeof(Enumerable))]publicstaticIOrderedQueryable<T>OrderDescending<T>(thisIQueryable<T>source,IComparer<T>comparer);}}
I don't think there is no way queryable can implement this new API in a more performant way than enumerable now does, as there is no documented way to introspect the comparer and somehow handle that on a repository layer.
In that case: is it useful to add the helper on Queryable?
@deeprobin The problem is, if the method is not added to Queryable, what happens if you call queryable.Order()? That code would be valid and would call Enumerable.Order(), since IQueryable<T> inherits from IEnumerable<T>. But then the ordering would not become part of the query, but would be instead executed in memory, which is not what you want.
Activity
kevingosse commentedon Mar 26, 2022
Shouldn't it return an
IOrderedEnumerable<T>for consistency withOrderBy?petroemil commentedon Mar 26, 2022
Could they be called
OrderandOrderDescendinginstead, dropping the past tense "ed" from the names?stephentoub commentedon Mar 26, 2022
If we add this, I think the name should just be Order rather than Ordered.
poke commentedon Mar 26, 2022
Maybe something explicit to avoid confusion with the existing methods:
OrderBySelfandOrderBySelfDescending(OrderByDescendingSelf?)Alternatively just
OrderBy()without any argument, defaulting to an identity function? 🤔CamiloTerevinto commentedon Mar 26, 2022
I think it would make more sense to allow for any T too, just like the existing APIs:
zoran-horvat commentedon Mar 26, 2022
I agree, with a note that "Ordered" implies that the method will return data in some form, but does not communicate that there will be a substantial cost disproportional to size of the input. "Order" is suggesting that there is work behind constructing the result.
ldardick commentedon Mar 26, 2022
Adding up, I feel explicitly defining the critetia (
OrderAscendingandOrderDescending) feels more natural.andi0b commentedon Mar 26, 2022
So it more or less comes down to instead of calling a LINQ method with an identity function (
x=>x) as parameter, just calling it without a parameter.There are also other LINQ methods that could use this API change. For example
SelectMany(x=>x).But also
ToDictionary(x=>x.key, x=>x.value)for a enumerable of tuples orKeyValuePairs(see #45679)terrajobst commentedon Mar 26, 2022
Updated proposal based on your feedback.
[-][API Proposal]: Enumerable.Ordered and OrderedDescending[/-][+][API Proposal]: Enumerable.Order and OrderDescending[/+]Joe4evr commentedon Mar 26, 2022
That almost happened (settled on the name
Flatten(), but it didn't make the cut for .NET 6: #54220).16 remaining items
bartonjs commentedon Jun 9, 2022
IEnumerable<int?>eiriktsarpalis commentedon Jun 9, 2022
Based on the decision made today, would it make sense to revisit
Flatten()as well?#54220 (comment)
stephentoub commentedon Jun 9, 2022
...and there's the slippery slope. 5 minutes... that took even less time than I expected ;-)
deeprobin commentedon Jun 9, 2022
Doesn't seem like a big change.
If you are ready for a community contribution here, feel free to assign me.
deeprobin commentedon Jun 10, 2022
@bartonjs
Originally posted by @rhuijben in #70525 (comment)
svick commentedon Jun 12, 2022
@deeprobin The problem is, if the method is not added to
Queryable, what happens if you callqueryable.Order()? That code would be valid and would callEnumerable.Order(), sinceIQueryable<T>inherits fromIEnumerable<T>. But then the ordering would not become part of the query, but would be instead executed in memory, which is not what you want.