Operator '??' cannot be applied to operands of type 'int' and 'int'
The variable on the left side of ??
operator has to be nullable (which means that you can assign null to it), in your case JobQuoteID
should be of type int?
not int
That is the null-coalescing operator, it only applies to nullable types, or rather the left hand side must be a nullable type (my language might be wrong there but when I say nullable i mean all Nullable<T>
's and reference types). If you had int?
instead of int
it would work. The operator is binary and works like so; a ?? b
says that if a
is null then use b for the value. You can chain this as many times as you'd like. So I could do int willNeverBeNull = a ?? b ?? c ?? 4
assuming a, b, and c are all nullable ints, it will take the first non null value.
The compiler is telling you that j.job_quote.JobQuoteID
is of type int
. An int
cannot be null, as it is a non-nullable value type. The ??
operator cannot be called on a type that is not nullable.