OData V4 .NET core nested properties cannot be translated from Linq

Viewed 379

I am trying to get the OData Query "orderby" on a nested property to work on .Net Core 3.1

According to my reading the correct query should be formatted like this:

https://{server}:{port}/v1/Entities?$expand=ownBy&$orderby=ownBy/userName

and I get the following error message:

 System.InvalidOperationException: The LINQ expression 'DbSet<Entity>
    .LeftJoin(
          outer: DbSet<User>, 
        inner: p => EF.Property<Nullable<Guid>>(p, "OwnById"), 
        outerKeySelector: u => EF.Property<Nullable<Guid>>(u, "UserId"), 
        innerKeySelector: (o, i) => new TransparentIdentifier<Portfolio, User>(
            Outer = o, 
            Inner = i
        ))
    .OrderBy(p => EF.Property<Nullable<Guid>>(p.Inner, "UserId") == null ? null : new UserModel{ 
        UserEmail = p.Inner.UserEmail, 
        UserFirstName = p.Inner.UserFirstName, 
        UserId = p.Inner.UserId, 
        UserLastName = p.Inner.UserLastName, 
        Username = p.Inner.Username 
    }
    .Username)' could not be translated.

It seems to be a fairly simple example and I don't understand why this is so complex to translate.

Here is my controller using automapper:

    [ODataRoute]
    [Produces("application/json")]
    [ProducesResponseType(typeof(ODataValue<IEnumerable<EntityModel>>), Status200OK)]
    [ProducesResponseType(Status404NotFound)]
    [EnableQuery(AllowedQueryOptions = All, MaxTop = 100)]
    public IQueryable<EntityModel> Get()
    {
        var results = _Query.Get();
        return results.ProjectTo<EntityModel>(_Mapper.ConfigurationProvider);
    }

Automapper config:

 CreateMap<Entity, EntityModel>();

Metadata:

    <EntityType Name="EntityModel">
        <Key>
            <PropertyRef Name="entityId" />
        </Key>
        ...
        <Property Name="ownById" Type="Edm.Guid" />
        ...
        <NavigationProperty Name="ownBy" Type="AMI.UserModel" />
    </EntityType>

    <EntityType Name="UserModel">
        <Key>
            <PropertyRef Name="userId" />
        </Key>
        <Property Name="userId" Type="Edm.Guid" Nullable="false" />
        <Property Name="username" Type="Edm.String" />
        <Property Name="userFirstName" Type="Edm.String" />
        <Property Name="userLastName" Type="Edm.String" />
        <Property Name="userEmail" Type="Edm.String" />
    </EntityType>

Any help would be very much appreciated.

0 Answers
Related