在OData查询中使用复杂类型

时间:2013-11-19 16:25:25

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

我正在尝试按照我的主要实体Address上的复杂类型Customer对数据进行排序。

public partial class Customer
{
    public Customer()
    {
        this.Address = new Address();
    }

    public string Name { get; set; }
    public Address Address { get; set; }
}

public partial class Address
{
    public string Address1 { get; set; }
    public string Address2 { get; set; }
    public string Town { get; set; }
    public string County { get; set; }
    public string PostCode { get; set; }
}

我查询了数据,我可以使用$sort=Name按名称对其进行排序,但是当我$sort=Address.PostCode时,我得到了:

"The child type 'Address.PostCode' in a cast was not an entity type. Casts can only be performed on entity types.","type":"Microsoft.Data.OData.ODataException","stacktrace":" at Microsoft.Data.OData.Query.DottedIdentifierBinder.BindDottedIdentifier(DottedIdentifierToken dottedIdentifierToken, BindingState state)\r\n at Microsoft.Data.OData.Query.MetadataBinder.BindCast(DottedIdentifierToken dottedIdentifierToken)\r\n at Microsoft.Data.OData.Query.MetadataBinder.Bind(QueryToken token)\r\n at Microsoft.Data.OData.Query.OrderByBinder.ProcessSingleOrderBy(BindingState state, OrderByClause thenBy, OrderByToken orderByToken)\r\n at Microsoft.Data.OData.Query.OrderByBinder.BindOrderBy(BindingState state, IEnumerable1 orderByTokens)\r\n at Microsoft.Data.OData.Query.ODataUriParser.ParseOrderByImplementation(String orderBy, IEdmType elementType, IEdmEntitySet entitySet)\r\n at System.Web.Http.OData.Query.OrderByQueryOption.get_OrderByClause()\r\n at System.Web.Http.OData.Query.OrderByQueryOption.get_OrderByNodes()\r\n at System.Web.Http.OData.Query.ODataQueryOptions.EnsureStableSortOrderBy(OrderByQueryOption orderBy, ODataQueryContext context)\r\n at System.Web.Http.OData.Query.ODataQueryOptions.ApplyTo(IQueryable query, ODataQuerySettings querySettings)\r\n at System.Web.Http.QueryableAttribute.ApplyQuery(IQueryable queryable, ODataQueryOptions queryOptions)\r\n at System.Web.Http.QueryableAttribute.ExecuteQuery(Object response, HttpRequestMessage request, HttpActionDescriptor actionDescriptor)\r\n at System.Web.Http.QueryableAttribute.OnActionExecuted(HttpActionExecutedContext actionExecutedContext)"

当我$sort=Address/PostCode时,我得到:

"Only ordering by properties at the root level is supported for non-primitive collections. Nested properties and expressions are not supported.","type":"Microsoft.Data.OData.ODataException","stacktrace":" at System.Web.Http.OData.Query.OrderByNode.CreateCollection(OrderByClause orderByClause)\r\n at System.Web.Http.OData.Query.OrderByQueryOption.get_OrderByNodes()\r\n at System.Web.Http.OData.Query.ODataQueryOptions.EnsureStableSortOrderBy(OrderByQueryOption orderBy, ODataQueryContext context)\r\n at System.Web.Http.OData.Query.ODataQueryOptions.ApplyTo(IQueryable query, ODataQuerySettings querySettings)\r\n at System.Web.Http.QueryableAttribute.ApplyQuery(IQueryable queryable, ODataQueryOptions queryOptions)\r\n at System.Web.Http.QueryableAttribute.ExecuteQuery(Object response, HttpRequestMessage request, HttpActionDescriptor actionDescriptor)\r\n at System.Web.Http.QueryableAttribute.OnActionExecuted(HttpActionExecutedContext actionExecutedContext)"

那么我该如何排序?

我正在使用Entity Framework v6和System.Web.Http.OData v5。

由于

1 个答案:

答案 0 :(得分:12)

嵌套属性上$ orderby的语法是后者,即应该$orderby=Address/PostCode

而且,我们对V2 RTM中的嵌套属性(当前位)没有$orderby的支持。但是,我们最近fixed我们的夜间版本中提供了此功能,并且可以在我们的下一版本中使用nuget。

要访问我们的夜间版本,您可以按照此link了解详细信息。