Export to GitHub

morelinq - issue #96

MaxByOrDefault


Posted on Dec 8, 2014 by Happy Monkey

Currently, MaxBy will throw an exception if executed on an empty enumerable. I would like a way to provide it with a default value to use, or have it use default(TSource).

Example usage: var emptyEnumerable = Enumerable.Empty<Person>(); var result = emptyEnumerable.MaxByOrDefault(person => person.Age); Assert.Equal(0, result);

result = emptyEnumerable.MaxBy(person => person.Age, 10); Assert.Equal(10, result);

Comment #1

Posted on Jul 25, 2015 by Helpful Giraffe

I also think that Max, Min, MaxBy and MinBy would benefit from having ...IsDefault methods.

I think though that if returning a value type, the return type should be a nullable if possible.

Comment #2

Posted on Jul 25, 2015 by Happy Monkey

If you want null out for a value type then you can achieve this by

emptyEnemurable.MaxByOrDefault>(person => person.Age); Assert.Equal(null, result);

or

emptyEnumerable.MaxBy>(person => person.Age, null); Assert.Equal(null, result);

Comment #3

Posted on Aug 21, 2015 by Massive Ox

This issue has been migrated to: https://github.com/MoreLINQ/morelinq/issues/96 The conversation continues there. DO NOT post any further comments to the issue tracker on Google Code as it is shutting down. You can also just subscribe to the issue on GitHub to receive notifications of any further development.

Status: New

Labels:
Type-Enhancement