Queries like the following:
Fail when joins are not successful (for this example: When _.OwnerVersion is null, VersionNumer is of type 'int' and not 'int?').
In this case the following Exception is thrown:
See https://github.com/re-motion/Relinq-SqlBackend/tree/bugfix/RMLNQSQL-113-NotNullable-Columns-Fail-When-Used-In-Nullable-Context for a repro-sample. Note that in the sample, the effect is not an exception but using the value types default instead of null.
The problem is created by SqlGeneratingOuterSelectExpressionVisitor.VisitNamed(). The NamedExpression's type is set to the not-nullable type because the type is inferred from the column's type, not the actual, converted, type. Possible solutions:
Automatically change a value type passed into the type parameter of IDatabaseResultRow.GetValue<T>() to a nullable value type. This way, the SQL Backend would always support null when loading the data. => npt recommended to keep the expression tree consistent.
Evaluate the projection to set the Type of the NamedExpression as a nullable value type in case the column is used as a nullable value. => pull the cast into the NamedExpression
Note: When no cast is provided, such a query construct must, by default, result in a null-conversion error since the result value of null cannot be converted to the value type used by the returned linq query.