如何$选择OData开放数据类型?查询无效错误

时间:2017-10-27 13:19:37

标签: c# .net asp.net-web-api odata

我正在为具有类型的开放数据类型的实体提供服务:

  

IDictionary

我无法使用$select关键字来选择该字典中的动态数据。我有其他静态属性$select就好了。我得到的错误如下:

    {
"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)"
    }
}

}

响应JSON是这样的:

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

以下查询均无效:

    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"

我的模特课是:

    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; }

   }
  }

我正在使用:

    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

有谁知道这可能是什么?开放类型是否可选?

2 个答案:

答案 0 :(得分:0)

古斯塔沃

它应该工作。查看我的新commit test

您是否记得在配置中启用Select功能?

答案 1 :(得分:0)

感谢您回答我,说它应该有用,它看起来更难。 正如Sam Xu所说,它适用于关键&#34; foo&#34;,我在我的dict上测试了它并且它起作用了。这让我觉得OData不会把数字作为关键,但它并没有。我通过附上&#39; a&#39;来解决我的问题。在每年的数字前面,并且知道它选择得很好。