As far as I can understand, the linq method FirstOrDefault() returns null if a record-set is empty. Why can’t use the ?? operator against the function? Like so:
Double d = new Double[]{}.FirstOrDefault() ?? 0.0;
Update
I don’t want to check if d is null later on in my code. And doing:
Double d new Double[]{}.FirstOrDefault() == null
? 0.0
: new Double[]{}.FirstOrDefault();
… or:
var r = new Double[]{}.FirstOrDefault();
Double d = r == null ? 0.0 : r;
… seems a bit overkill–I’d like to do this null-check in one line of code.
Actually,
FirstOrDefault<T>()returns T, which is either a value ordefault(T).default(T)is eithernullor(T)0for value types (likedouble)