Skip to content

Custom Method with nullable properties #1084

@bjelbo

Description

@bjelbo

I am experiencing problems when creating Custom Methods that should handle nullable properties.

My database entity contains nullable properties. e.g.

public DateTime? SomeDateTime { get; set; }

My custom method also takes a nullable DateTime as argument. e.g.

public static Date? DateOnly(DateTime? date)
{
    if (date == null)
    {
        return null;
    }
    return date.Value.ToUniversalTime().Date;
}

An OData query with the following compute statement throws an exception.

$apply=compute(DateOnly(SomeDateTime) as date)

The Exception is:

"'Expression of type 'System.DateTime' cannot be used for parameter of type 'System.Nullable`1[System.DateTime]' of method 'System.Nullable`1[Microsoft.OData.Edm.Date] DateOnly(System.Nullable`1[System.DateTime])' (Parameter 'arg0')"

The problem lies in the method ExpressionBinderHelper.MakeFunctionCall

In this method line 228 the following method is called

// if the argument is of type Nullable<T>, then translate the argument to Nullable<T>.Value as none
// of the canonical functions have overloads for Nullable<> arguments.
functionCallArguments = ExtractValueFromNullableArguments(functionCallArguments);

Then when calling Expression.Call it will fail because 'System.DateTime' is not System.Nullable`1[System.DateTime]

Metadata

Metadata

Labels

bugSomething isn't working

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions