Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

MaxByOrDefault #96

Open
GoogleCodeExporter opened this issue Aug 13, 2015 · 2 comments
Open

MaxByOrDefault #96

GoogleCodeExporter opened this issue Aug 13, 2015 · 2 comments

Comments

@GoogleCodeExporter
Copy link

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);

Original issue reported on code.google.com by mi...@zoltu.net on 8 Dec 2014 at 8:12

@GoogleCodeExporter
Copy link
Author

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.

Original comment by rmy...@gmail.com on 25 Jul 2015 at 12:36

@GoogleCodeExporter
Copy link
Author

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

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

or

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

Original comment by mi...@zoltu.net on 25 Jul 2015 at 5:19

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant