rsdn / CodeJam

Set of handy reusable .NET components that can simplify your daily work and save your time when you copy and paste your favorite helper methods and classes from one project to another
MIT License
258 stars 35 forks source link

EnumerableExtensions.MinBy and MaxBy throw when collection has only null values #135

Open NN--- opened 3 years ago

NN--- commented 3 years ago
string?[] a = { null, null };
var m = a.MinBy(x => x); // throws no InvalidOperationException collection has no elements.

This happens since the methods looks for non default value while the correct is to have an indicator for not empty enumeration.

NN--- commented 3 years ago

https://github.com/microsoft/referencesource/blob/5697c29004a34d80acdaf5742d7e699022c64ecd/System.Core/System/Linq/Enumerable.cs#L1540

NN--- commented 3 years ago

https://docs.microsoft.com/en-us/dotnet/api/system.linq.enumerable.minby?view=net-6.0 https://docs.microsoft.com/en-us/dotnet/api/system.linq.enumerable.maxby?view=net-6.0