gpt4 book ai didi

c# - 如何在 asp.net mvc web api 中为返回动态的 OData 服务启用排序

转载 作者:行者123 更新时间:2023-11-30 12:30:37 38 4
gpt4 key购买 nike

这是我的 Controller 的代码:

public class RegularGrigController : ApiController
{
// GET api/regulargrig
public IEnumerable<string> Get()
{
return EntityHelper.GetEntities().Select(t => t.ModelName());
}

public PageResult<dynamic> Get(string entityName, ODataQueryOptions options)
{
var query = EntityHelper.GetQueryable(entityName).Select("new (Id, SysName)"); // Dynamic queryable

IQueryable results = options.ApplyTo(query); // Exception here

return new PageResult<dynamic>(results as IEnumerable<dynamic>, Request.GetNextPageLink(), Request.GetInlineCount());
}
}

当我尝试调用 /api/RegularGrig?entityName=SecurityPrincipal&$orderby=Id

我收到这个异常

Type 'System.Object' does not have a property 'Id'

那么,有没有办法对动态数据进行排序/过滤呢?

最佳答案

这段代码现在对我有用:

public PageResult<dynamic> Get(string entityName)
{
var query = EntityHelper.GetQueryable(entityName).Select("new (Id, SysName)");
var modelBuilder = new ODataConventionModelBuilder();
modelBuilder.AddEntity(query.ElementType);
var model = modelBuilder.GetEdmModel();
var options = new ODataQueryOptions(new ODataQueryContext(model, query.ElementType), this.Request);
IQueryable results = options.ApplyTo(query);

return new PageResult<dynamic>(results as IEnumerable<dynamic>, Request.GetNextPageLink(), Request.GetInlineCount());
}

关于c# - 如何在 asp.net mvc web api 中为返回动态的 OData 服务启用排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15764215/

38 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com