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

New methods MinOrDefault and MaxOrDefault #28

Open
GoogleCodeExporter opened this issue Dec 3, 2015 · 4 comments
Open

New methods MinOrDefault and MaxOrDefault #28

GoogleCodeExporter opened this issue Dec 3, 2015 · 4 comments

Comments

@GoogleCodeExporter
Copy link

When you start playing with LINQ queries over sequences of elements (e.g.
getting min / max value for enumerable source) sooner or later you will
come across this one -- the InvalidOperationException (“Sequence contains
no elements”).

The problem occurs as by default queries like IEnumerable<T>.Min(…) and
IEnumerable<T>.Max(…) do not play nicely if you try to execute them on an
empty sequence and just throw the exception described above. Unfortunately
these methods do not have a corresponding counterpart like Single(…) /
SingleOrDefault(…) that is smart enough to query the sequence if it is not
empty or alternatively use the default value without raising an exception.

Basically you got two options now:

    * Either perform the check on the enumerable sequence every time you
are querying it
    * OR integrate the logic in an extension method.

The second approach is much preferable so let’s add the missing link.

http://blogs.telerik.com/manoldonev/posts/08-10-17/linq_sequence_contains_no_ele
ments_extension_methods_to_the_rescue.aspx

Original issue reported on code.google.com by anton.ge...@gmail.com on 17 Sep 2009 at 8:56

@GoogleCodeExporter
Copy link
Author

Compelling feature!

Original comment by Weitzhan...@gmail.com on 23 Nov 2012 at 8:37

@GoogleCodeExporter
Copy link
Author

I need this right now !

Original comment by rdingw...@gmail.com on 5 Dec 2012 at 1:04

@GoogleCodeExporter
Copy link
Author

You can implement this easily with a combination of using nullables and 
Prepend/Concat a null before calling  Min/Max, like this:

var some = Enumerable.Range(10, 10);
var none = Enumerable.Range(10,  0);
Console.WriteLine(some.Cast<int?>().Prepend(null).Min()); // prints 10
Console.WriteLine(none.Cast<int?>().Prepend(null).Min()); // prints null
Console.WriteLine(some.Cast<int?>().Prepend(null).Max()); // prints18
Console.WriteLine(none.Cast<int?>().Prepend(null).Max()); // prints null

Once you have a null you can use ?? fault in a value. The implementation in the 
blog article[1] suffers from iterating the sequence twice.

[1] 
http://blogs.telerik.com/xamlteam/posts/08-10-17/linq-sequence-contains-no-eleme
nts-extension-methods-to-the-rescue.aspx

Original comment by azizatif on 22 Jun 2013 at 12:15

  • Added labels: Type-Enhancement
  • Removed labels: Type-Defect

@GoogleCodeExporter
Copy link
Author

This issue has been migrated to:
https://github.com/MoreLINQ/morelinq/issues/28
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.

Original comment by azizatif on 21 Aug 2015 at 6:55

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