sawbona / morelinq

Automatically exported from code.google.com/p/morelinq
Apache License 2.0
0 stars 0 forks source link

MinBy, MaxBy corrections #75

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
Could you please consider some correction for methods MinBy and MaxBy? I think 
it would be more comfortable if these methods would return just null value if 
input sequence was empty instead of throwing an InvalidOperationException.

Example:

var prevItemByCategory = galleryCategory.Items
   .Where(i => i.GalleryIndex < itemByCategory.GalleryIndex)
   .MaxBy(i => i.GalleryIndex);

According to example above I want just to check whether previous item is null 
when it is needed instead of try-catching this piece of code.

Best regards,
Nikolay Dikiy

Original issue reported on code.google.com by nikolay....@gmail.com on 27 Aug 2012 at 10:37

GoogleCodeExporter commented 9 years ago
But, if they return default(T), they would behave differently than regular Max 
and Min, who do throw invalidoperationexception ("Sequence contains no 
elements").

Original comment by tormod.s...@gmail.com on 27 Aug 2012 at 10:54

GoogleCodeExporter commented 9 years ago
Won't fix since, as per comment #1, it would be surprising for By versions of 
the methods to behave differently than their non-By counterparts. You can 
achieve the desired effect with DefaultIfEmpty combined with a guard in MinBy, 
like this:

var ps = new System.Diagnostics.Process[] {};
var min = ps.DefaultIfEmpty()
            .MaxBy(p => p != null ? p.StartTime : (DateTime?) null);

Original comment by azizatif on 28 Dec 2012 at 10:47