WebApi
WebApi copied to clipboard
Support nullable (alternate) keys in ODataPathQueryBuilder
Related: OData/odata.net#3415
Currently if I have a nullable property (like int?) used in an alternate key ODataPathQueryBuilder throws an exception when processing the generated KeySegment (with an int constant value):
System.InvalidOperationException: The binary operator Equal is not defined for the types 'System.Nullable`1[System.Int32]' and 'System.Int32'.
at System.Linq.Expressions.Expression.GetEqualityComparisonOperator(ExpressionType binaryType, String opName, Expression left, Expression right, Boolean liftToNull)
at System.Linq.Expressions.Expression.Equal(Expression left, Expression right, Boolean liftToNull, MethodInfo method)
at ...\ODataPathQueryBuilder.cs:line 69
...
Replacing: https://github.com/OData/WebApi/blob/1b8f45e74a791784fe7cac071320bf70469f48a5/src/Microsoft.AspNet.OData.Shared/Query/ODataPathQueryBuilder.cs#L68-L71 With:
IEnumerable<BinaryExpression> conditions = keySegment.Keys.Select(kvp => {
Expression prop = Expression.Property(filterParam, kvp.Key);
Expression cons = Expression.Constant(kvp.Value);
if (prop.Type != cons.Type)
cons = Expression.Convert(cons, prop.Type);
return Expression.Equal(prop, cons);
});
Seems to solve the issue