【问题标题】:handling selects in odataqueryoptions in core?在核心的 odataqueryoptions 中处理选择?
【发布时间】:2017-11-03 21:18:16
【问题描述】:

我有一个常规控制器,它具有 odata 功能:

[Route("[controller]")]
public class ChannelsController : Controller
{
    private readonly ChannelContext db = new ChannelContext();
    public IQueryable<PrdChannel> Get()
    {
        var entities = db.PrdChannel.AsQueryable();
        var modelManager = (IODataModelManger) HttpContext.RequestServices.GetService(typeof(IODataModelManger));
        var model = modelManager.GetModel(nameof(ProductConfiguration));
        var queryContext = new ODataQueryContext(model, typeof(PrdChannel), null);
        var queryOptions = new ODataQueryOptions(queryContext, HttpContext.Request);

        return queryOptions
            .ApplyTo(entities, new ODataQuerySettings
            {
                HandleNullPropagation = HandleNullPropagationOption.True
            })
            .Cast<PrdChannel>();
    }
}

我可以发出 GET:https://myservice/Channel?$filter=cable lt 10

但是,当我尝试执行$select 时,它不起作用!

https://myservice/Channel?$select=cable

根据OdataQueryOptions的定义,不支持选择/展开。

问题:我们如何在 aspnetcore 中支持 $select

也许这应该是一个 ODataController?

【问题讨论】:

标签: c# .net asp.net-core visual-studio-2017 odata


【解决方案1】:

重复:Can't apply $select when using ODataQueryOptions

要在 OData 中支持 $select,您不能将 IQueryable 转换为定义的类型,您需要改用 dynamic 类型。

    public IQueryable<dynamic> Get()
    {
        ... your code ...
        return queryOptions
            .ApplyTo(entities, new ODataQuerySettings
            {
                HandleNullPropagation = HandleNullPropagationOption.True
            })
            as IQueryable<dynamic>;
    }

【讨论】:

    猜你喜欢
    • 2023-03-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-05-01
    • 1970-01-01
    相关资源
    最近更新 更多