Search code examples
c#asp.net-coreodata

Only include certain fields from expanded entity


I have an asp.net core project with Microsoft.AspNetCore.OData 8.2.3 on top of Entity Framework Core. My entities are Order and Customer which have a relation.

I can successfully load my Orders with the customer data with the url

https://localhost:7065/api/Order?$expand=Customer

But this will give me all Order and Customer fields. I can successfully limit the Order fields if I add a $select=Id,Orderno

https://localhost:7065/api/Order?$expand=Customer&$select=Id,Orderno

but I will still get all Customer fields. According to learn.microsoft.com it should be possible to write

https://localhost:7065/api/Order?$expand=Customer&$select=Id,Orderno,Customer/Name

but I get an exception result

{
  "error": {
    "code": "",
    "message": "The query specified in the URI is not valid. Found a path with multiple navigation properties or a bad complex property path in a select clause. Please reword your query such that each level of select or expand only contains either TypeSegments or Properties.",
    "details": [],
    "innererror": {
      "message": "Found a path with multiple navigation properties or a bad complex property path in a select clause. Please reword your query such that each level of select or expand only contains either TypeSegments or Properties.",
      "type": "Microsoft.OData.ODataException",
      "stacktrace": "   at Microsoft.OData.UriParser.SelectExpandBinder.ProcessSelectTokenPath(PathSegmentToken tokenIn)\r\n   at Microsoft.OData.UriParser.SelectExpandBinder.GenerateSelectItem(SelectTermToken tokenIn)\r\n   at Microsoft.OData.UriParser.SelectExpandBinder.Bind(ExpandToken expandToken, SelectToken selectToken)\r\n   at Microsoft.OData.UriParser.SelectExpandSemanticBinder.Bind(ODataPathInfo odataPathInfo, ExpandToken expandToken, SelectToken selectToken, ODataUriParserConfiguration configuration, BindingState state)\r\n   at Microsoft.OData.UriParser.ODataQueryOptionParser.ParseSelectAndExpandImplementation(String select, String expand, ODataUriParserConfiguration configuration, ODataPathInfo odataPathInfo)\r\n   at Microsoft.OData.UriParser.ODataQueryOptionParser.ParseSelectAndExpand()\r\n   at Microsoft.AspNetCore.OData.Query.Validator.SelectExpandQueryValidator.Validate(SelectExpandQueryOption selectExpandQueryOption, ODataValidationSettings validationSettings)\r\n   at Microsoft.AspNetCore.OData.Query.Validator.ODataQueryValidator.Validate(ODataQueryOptions options, ODataValidationSettings validationSettings)\r\n   at Microsoft.AspNetCore.OData.Query.EnableQueryAttribute.ValidateQuery(HttpRequest request, ODataQueryOptions queryOptions)\r\n   at Microsoft.AspNetCore.OData.Query.EnableQueryAttribute.OnActionExecuting(ActionExecutingContext actionExecutingContext)"
    }
  }
}

I also tried escaping the slash which doesn't work either

https://localhost:7065/api/Order?$expand=Customer&$select=Id,Orderno,Customer%2FName

How do I have to rewrite the url to only get desired fields from the customer?


Solution

  • I would use nested $select inside the $expand

    https://localhost:7065/api/Order?$expand=Customer($select=Name)&$select=Id,Orderno