问题
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