[ACCEPTED]-Return null for FirstOrDefault() on empty IEnumerable<int>?-linq

Accepted answer
Score: 55
int? nullableId = GetNonNullableInts().Cast<int?>().FirstOrDefault();

0

Score: 1

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

More Related questions