1

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?

kdelmonte
  • 680
  • 1
  • 10
  • 16

1 Answers1

2

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;
}
Steve Schmitt
  • 3,124
  • 12
  • 14