Skip to content

Support nullable (alternate) keys in ODataPathQueryBuilder #2888

@Xriuk

Description

@Xriuk

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:

IEnumerable<BinaryExpression> conditions = keySegment.Keys.Select(kvp =>
Expression.Equal(
Expression.Property(filterParam, kvp.Key),
Expression.Constant(kvp.Value)));

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions