kdelmonte
kdelmonte

Reputation: 680

BreezeJS: Applying Client Query in Controller

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?

Upvotes: 1

Views: 161

Answers (1)

Steve Schmitt
Steve Schmitt

Reputation: 3209

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;
}

Upvotes: 2

Related Questions