[ACCEPTED]-Return null for FirstOrDefault() on empty IEnumerable<int>?-linq
Accepted answer
int? nullableId = GetNonNullableInts().Cast<int?>().FirstOrDefault();
0
FirstOrDefault
depends on T
from IEnumerable<T>
to know what type to 3 return, that's why you're receiving int
instead 2 int?
.
So you'll need to cast your items to int?
before 1 return any value, just like Matt said
Source:
stackoverflow.com
More Related questions
Cookie Warning
We use cookies to improve the performance of the site. By staying on our site, you agree to the terms of use of cookies.