Question: The line price = co?.price ?? 0,
in the following code gives me the above error. but if I remove ?
from co.?
The code you link to uses List
. List
implements IEnumerable
but not IQueryable
. In that case, the projection is executed in memory and ?.
works.
You're using some IQueryable
, which works very differently. For IQueryable
, a representation of the projection is created, and your LINQ provider decides what to do with it at runtime. For backwards compatibility reasons, ?.
cannot be used here.
Depending on your LINQ provider, you may be able to use plain .
and still not get any NullReferenceException
.