Weslley Leandro
Weslley Leandro

Reputation: 21

$orderBy with duplicate property in odata v4

I have the following models:

public class Field
{
    public string Name { get; set; }
    public int Order { get; set; }

    public int FieldGroupId { get; set; }
}

public class FieldGroup
{
    public string Name { get; set; }
    public int Order { get; set; }

    public virtual ICollection<Field> Field { get; set; }
}

And what I am want to do is a query where I can order first by the group and then by the field, like:

http://localhost:44300/API/odata/IntFieldSet?$count=true&$expand=FieldGroup&$orderby=FieldGroup/Order,Order

However, when I try it I receive the following error:

Duplicate property named 'Order' is not supported in '$orderby'.

Although they have the same name, they are not the same field. If I try order by fields with different names or only one of those per time, it works. Ex:

$orderby=FieldGroup/Order,Name   ||   $orderby=FieldGroup/Order   ||   $orderby=Order

It used to work on odata v3 but not anymore that I am using odata v4.

Anyone have faced this problem before. Can you help me to find out a solution?

Thanks in advance.

Upvotes: 2

Views: 1793

Answers (2)

smoksnes
smoksnes

Reputation: 10851

It is fixed now. The correct query should look something like:

http://localhost:44300/API/odata/IntFieldSet?$count=true&$expand=FieldGroup&$orderby=FieldGroup/Order asc,Order desc

It will rougly be translate to:

.OrderBy(x => x.FieldGroup.Order).ThenByDescending(x => x.Order);

Here's an example from odata.org:

http://services.odata.org/TripPinRESTierService/Airports?$orderby=Name%20asc,%20Location/Address%20desc

Upvotes: 0

Alexander Vasilyev
Alexander Vasilyev

Reputation: 1313

I have the same issue using WebAPI OData 5.6 and 5.7. However they say they fixed it in version WebAPI Odata 5.7: https://github.com/OData/WebApi/issues/376 Will wait for publication of the update.

Upvotes: 1

Related Questions