You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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
The text was updated successfully, but these errors were encountered:
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
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
Original issue reported on code.google.com by
anton.ge...@gmail.com
on 17 Sep 2009 at 8:56The text was updated successfully, but these errors were encountered: