如何在ASP中使用TypeLess(UnTyped)对象的情况下使用ODataQueryOptions.NET WebA
本文关键字:对象 情况下 WebA NET ODataQueryOptions ASP TypeLess UnTyped | 更新日期: 2023-09-27 18:01:13
我一直在引用Asp.net WebApi 2.2对OData的支持。WebApi处理许多OData V4协议相关的事情,这非常有趣。
我需要实现一个没有CLR对象的Odata服务。我在运行时了解了类的属性。这就像我有存储Sql查询的xml文件。我读取这些xml文件并执行其中的查询。在阅读了XML文件之后,我实现了这些列,并希望在OData服务下公开这些信息。
我一直面临的挑战是,我无法将ODataQueryOptions应用于非类型化(非CLR(对象。
示例代码https://aspnet.codeplex.com/SourceControl/latest#Samples/WebApi/OData/v4/ODataUntypedSample/ReadMe.txt显示了如何从WebApi OData服务公开未类型化的对象,但没有显示如何应用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());
}
//*/
}
如果有人能给我一种方法,让一些框架或插件可以处理OData查询选项(筛选、选择、排序等(,我将不胜感激。
恐怕您必须自己应用它们。OData只会在您公开具体类型的IQueryable(通常通过EF(时为您应用它们,但如果您没有,它将不知道如何应用它,因为您可能从任何地方获取数据!