BreezeJS: Applying Client Query in Controller

守給你的承諾、 提交于 2019-12-02 07:04:56

问题


Is there anyway to apply the user query in the controller in order to perform some actions to the final result set?

Take the following example:

[HttpGet]
public IQueryable<Container> Containers(bool populate)
{
    var containers = _contextProvider.Context.Containers;
    if (populate)
    {
         foreach (var container in containers)
         {
             container.Populate(_contextProvider.Context);
         }
    }
    return containers;
}

The problem here is that I am doing this Populate() action to all records in this table instead of just the ones that the user requested because their query has not been applied yet. How can I achieve this?


回答1:


You need to get the ODataQueryOptions passed into your method, so you can apply them manually instead of letting WebApi apply them on the way out.

[HttpGet]
public IQueryable<Container> Containers(ODataQueryOptions options, bool populate)
{
    IQueryable<Container> containers = _contextProvider.Context.Containers;
    containers = options.ApplyTo(Containers).Cast<Container>();
    if (populate)
    {
        foreach (var container in containers)
        {
            container.Populate(_contextProvider.Context);
        }
    }
    return containers;
}


来源:https://stackoverflow.com/questions/29621322/breezejs-applying-client-query-in-controller

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!