Reputation: 770
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. 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.
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.
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
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.
In my Api controller, I have also tried IQueryable< MyObject> but that doesnt make a difference
Upvotes: 2
Views: 5807
Reputation: 6793
Aspnet Web API OData doesn't support ordering by nested properties or expressions. There is an issue open on codeplex for supporting ordering by nested properties.
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.
Upvotes: 1
Reputation: 4336
In general, OData as a protocol allows the second thing you tried (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).
Upvotes: 4