简体   繁体   English

使用OData的Orderby复杂类型

[英]Orderby complex type using OData

I am starting to use OData in my MVC4 application and the problem that I am having, is that I cannot perform any sort or filter operations on my IQueryable because I am using complex objects. 我开始在MVC4应用程序中使用OData,而我遇到的问题是,由于使用复杂的对象,我无法对IQueryable执行任何排序或过滤操作。 Below is a simple example of something I am trying to accomplish: 以下是我要完成的工作的简单示例:

My API Controller is attempting to return a collection of MyObjects as IQueryable. 我的API控制器正在尝试将MyObjects的集合返回为IQueryable。

public IQueryable Get()
{
    List<MyObject> myObjects = GetMyObjects();
    return myObjects.AsQueryable() ;
}

Each MyObject contains an InnerObject that has the properties I want to sort and/or filter on. 每个MyObject都包含一个InnerObject,它具有我要对其进行排序和/或筛选的属性。

public class MyObject
{
    [Key]
    public MyInnerObject innerObject{ get; set; }

    public MyObject(Dictionary<string, object> value)
    {
        innerObject= new MyInnerObject(){
            item = value["item"].ToString(),
            itmdesc = value["itmdesc"].ToString()
        };
    }
}

public class MyInnerObject
{
    public string item { get; set; }
    public string itmdesc { get; set; }
}

I can successfully execute the top commands through the url 我可以通过url成功执行最重要的命令

localhost:5050/Test/Get?$top=10

But I really want to be able to sort my results using 但是我真的很希望能够使用

localhost:5050/Test/Get?$top=10&$orderby=innerObject.item

I have tried 我努力了

localhost:5050/Test/Get?$top=10&$orderby=innerObject.item
localhost:5050/Test/Get?$top=10&$orderby=innerObject/item
localhost:5050/Test/Get?$top=10&$orderby=item

Any suggestions? 有什么建议么?

EDIT: 编辑:

I should mention that it works if I put the item and itmdesc properties within MyObject, but for my purposes (this is just a minified version of my complex entities), they will need to be wrapped in a complex type. 我应该提到的是,如果将item和itmdesc属性放在MyObject中,它将起作用,但是出于我的目的(这只是我的复杂实体的精简版),它们将需要包装为复杂类型。

In my Api controller, I have also tried IQueryable< MyObject> but that doesnt make a difference 在我的Api控制器中,我也尝试过IQueryable <MyObject>,但这没什么不同

In general, OData as a protocol allows the second thing you tried ( localhost:5050/Test/Get?$top=10&$orderby=innerObject/item ). 通常,OData作为协议允许您尝试第二件事( localhost:5050/Test/Get?$top=10&$orderby=innerObject/item )。 It is likely that this is a temporary limitation of the Web API implementation of OData (assuming that's what you're using based on the rest of your environment and the returning of IQueryable). 这很可能是OData的Web API实现的临时限制(假设您是根据环境的其余部分以及IQueryable的返回来使用的)。

Aspnet Web API OData doesn't support ordering by nested properties or expressions. Aspnet Web API OData不支持按嵌套属性或表达式排序。 There is an issue open on codeplex for supporting ordering by nested properties. Codeplex上存在一个支持嵌套属性排序的问题

However if you are slightly adventurous you can use ODataQueryOptions to model bind the individual odata query options and then translate $orderby AST to a linq expression and apply it manually to your IQueryable. 但是,如果您有点冒险,则可以使用ODataQueryOptions对单个odata查询选项进行模型绑定,然后将$ orderby AST转换为linq表达式,然后将其手动应用于IQueryable。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM