How to $select on OData Open Data Type? Query not

2019-08-25 08:19发布

问题:

I'm serving an entity that has a Open Data Type of type:

IDictionary

I'm not able to use the $select keyword to select the dynamic data I have in that dictionary. I have other static atributtes that $select just fine. The error I'm getting is the following:

    {
"error": {
    "code": "",
    "message": "The query specified in the URI is not valid. An identifier was expected at position 0.",
    "innererror": {
        "message": "An identifier was expected at position 0.",
        "type": "Microsoft.OData.ODataException",
        "stacktrace": "   em Microsoft.OData.UriParser.ExpressionToken.GetIdentifier()\r\n   em Microsoft.OData.UriParser.SelectExpandTermParser.ParseSegment(PathSegmentToken previousSegment, Boolean allowRef)\r\n   em Microsoft.OData.UriParser.SelectExpandTermParser.ParseTerm(Boolean allowRef)\r\n   em Microsoft.OData.UriParser.SelectExpandParser.ParseSingleSelectTerm()\r\n   em Microsoft.OData.UriParser.SelectExpandParser.ParseCommaSeperatedSelectList(Func`2 ctor, Func`1 termParsingFunc)\r\n   em Microsoft.OData.UriParser.SelectExpandParser.ParseSelect()\r\n   em Microsoft.OData.UriParser.SelectExpandSyntacticParser.Parse(String selectClause, String expandClause, IEdmStructuredType parentEntityType, ODataUriParserConfiguration configuration, ExpandToken& expandTree, SelectToken& selectTree)\r\n   em Microsoft.OData.UriParser.ODataQueryOptionParser.ParseSelectAndExpandImplementation(String select, String expand, ODataUriParserConfiguration configuration, ODataPathInfo odataPathInfo)\r\n   em Microsoft.OData.UriParser.ODataQueryOptionParser.ParseSelectAndExpand()\r\n   em System.Web.OData.Query.SelectExpandQueryOption.get_SelectExpandClause()\r\n   em System.Web.OData.Query.Validators.SelectExpandQueryValidator.Validate(SelectExpandQueryOption selectExpandQueryOption, ODataValidationSettings validationSettings)\r\n   em System.Web.OData.Query.SelectExpandQueryOption.Validate(ODataValidationSettings validationSettings)\r\n   em System.Web.OData.Query.Validators.ODataQueryValidator.Validate(ODataQueryOptions options, ODataValidationSettings validationSettings)\r\n   em System.Web.OData.Query.ODataQueryOptions.Validate(ODataValidationSettings validationSettings)\r\n   em System.Web.OData.EnableQueryAttribute.ValidateQuery(HttpRequestMessage request, ODataQueryOptions queryOptions)\r\n   em System.Web.OData.EnableQueryAttribute.ExecuteQuery(Object response, HttpRequestMessage request, HttpActionDescriptor actionDescriptor, ODataQueryContext queryContext)\r\n   em System.Web.OData.EnableQueryAttribute.OnActionExecuted(HttpActionExecutedContext actionExecutedContext)"
    }
}

}

The response JSON is something like this:

    "@odata.context": "http://localhost:60275/odata4/$metadata#AnoValors",
"value": [
    {
        "1985": 13.822407187575308,
        "1986": 14.37878785150181,
        "1987": 14.914295678631518,
        "1988": 14.671386324309562,
        "ID": "IDDATA",            
    },...

None of the following queries work:

    http://localhost:60275/odata4/ControllerName(ID='IDData')/?$select=1985
    http://localhost:60275/odata4/ControllerName(ID='IDData')?$select=1985
    http://localhost:60275/odata4/ControllerName(ID='IDData')/?$select='1985'
    http://localhost:60275/odata4/ControllerName(ID='IDData')/?$select="1985"

My model class is:

    using System;
    using System.Collections.Generic;
    using System.ComponentModel.DataAnnotations;
    using System.ComponentModel.DataAnnotations.Schema;

    namespace OdataV4.Models{

    public class YearValue
    {      

        [Key]
        public string ID{ get; set; }
        public IDictionary<string, object> YEARVALUE { get; set; }

   }
  }

I'm using:

    Entity Framework 6.1.3
    Microsoft.AspNet.OData 6.0.0
    Microsoft.AspNet.WebApi.OData 5.7.0
    Microsoft.AspNet.WebApi 5.0.0
    Microsoft.Data.OData 5.0.0
    Microsoft.OData.Core 7.0.0
    Microsoft.OData.Edm 7.0.0

Does anyone knows what this could be? Are Open Types selectable?

回答1:

Gustavo

It should work. See my new commit test.

Did you remember to enable the Select functionality in the configuration?



回答2:

Thank you for answering me, saying that it should work, it made look harder. As Sam Xu noted, it works with the key "foo", and I tested it on my dict and it worked. It made me think that OData doens't take a number as a key, and it doesn't. I solved my problem by appending the letter 'a' in front of each year number, and know it selects just fine.