queryable Take:1] singleOrDefault] throws exception for multiple results
Codeglee opened this issue · 6 comments
I assume this is unintended behaviour, I hadn't expected it.
Great project though.
Thanks for the bug report. That's definitely not supposed to happen! I'll have a look.
Not sure what I was talking about above, but this is actually the "correct" behavior, as defined by .NET's Enumerable.SingleOrDefault(). If there is more than one record, single and singleOrDefault throw an exception - the 'single' is essentially stating that you are expecting there to be a single record in the list.
If you don't want an exception to be thrown in this case, you can use first/firstOrDefault instead.
Hopefully that clears things up! It's definitely not very intuitive, though.
That's confusing, if I call Take:1 then I am very much expecting there to be a single record in the list.
Calling singleOrDefault in this case shouldn't fail.
In LINQ, this returns a single result without complaint:
List strings = new List( new string[] { "A", "B", "C" });
strings.Take(1).SingleOrDefault();
On second thought, you are right, of course! Apparently I was not on the ball yesterday....
I've just pushed a fix. Thanks again for the bug report!
Great, thanks.