问题描述:

I have been referring Asp.net WebApi 2.2 support for OData. It is very interesting where WebApi handles lot of OData V4 protocol related things.

I need to implement an Odata service where I dont have CLR object. I get to know about properties of my class at runtime. Its like I have xml files which store Sql queries. I read those xml files and execute the queries in them. After reading the XML file I realize the columns and would like expose this information under OData service.

Challenge I have been facing is that I am not able to apply ODataQueryOptions on untyped (non CLR) objects.

Sample code https://aspnet.codeplex.com/SourceControl/latest#Samples/WebApi/OData/v4/ODataUntypedSample/ReadMe.txt shows how to expose untyped object from WebApi OData service but it does not show how to apply ODataQueryOptions.

public class ProductsController : ODataController

{

private static IQueryable<IEdmEntityObject> Products = Enumerable.Range(0, 20).Select(i =>

{

IEdmEntityType productType = (IEdmEntityType)ODataUntypedModel.Model.FindType("AnalyticsPortal.Product");

EdmEntityObject product = new EdmEntityObject(productType);

product.TrySetPropertyValue("Id", i);

product.TrySetPropertyValue("Name", "Product " + i);

product.TrySetPropertyValue("Price", i + 0.01);

product.TrySetPropertyValue("Category", "Category - " + i);

return product;

}).AsQueryable();

///*

public EdmEntityObjectCollection Get()

{

//return productsContext.Products.AsQueryable();

var path = Request.ODataProperties().Path;

var edmType = path.EdmType;

var collectionType = edmType as IEdmCollectionType;

var entityType = collectionType.ElementType.Definition as IEdmEntityType;

var model = Request.ODataProperties().Model;

var queryContext = new ODataQueryContext(model, entityType, path);

var queryOptions = new ODataQueryOptions(queryContext, Request);

//Apply the query option on the IQueryable here.

//queryOptions

//How ??

//queryOptions.ApplyTo() work only on CLR types

//IQueryable<IEdmEntityObject>

return new EdmEntityObjectCollection(new EdmCollectionTypeReference(collectionType), Products.ToList());

}

//*/

}

I would appreciate if someone can point me to a way where some framework or plugin can handle OData query options (filter, select, order etc...)

网友答案:

I'm afraid that you have to apply them yourself. OData will only apply them for you if you expose an IQueryable of a concrete type (typically through EF) but if you don't have that then it won't know how to apply it as you could be getting your data from anywhere!

相关阅读:
Top