2
votes

I am trying to get the $select portion of ODataQueryOptions working with my Business layer and I can't seem to noodle way (fyi: placing the EF query in my controller is not an option)..

Here is my ODataController method

public async Task<IHttpActionResult> Get(Guid propertyId, ODataQueryOptions<HighSchoolViewModel> odataOptions)
    {
        // validate the query.
        try
        {
            odataOptions.Validate(_validationSettings);
        }
        catch (ODataException ex)
        {
            return BadRequest(ex.Message);
        }

        IEnumerable<HighSchoolViewModel> returnData = await _service.ODataSearchAsync(propertyId, odataOptions);
        return Ok<IEnumerable<HighSchoolViewModel>>(returnData);
    }

And here is my business layer/method

public async Task<IEnumerable<HighSchoolViewModel>> ODataSearchAsync(Guid propertyId, ODataQueryOptions<HighSchoolViewModel> queryOptions)
        {
            using (DbContext context = new LAMSContext())
            {
                var query = queryOptions.ApplyTo(context.PropertyHighSchools.Where(hs => hs.PropertyId == propertyId)
                     .Project().To<HighSchoolViewModel>());
                return await ((IQueryable<HighSchoolViewModel>)query).ToListAsync();
            }
        }

This works great for everything except the $select (and likely the expand but that's "out of scope").

Obviously when the ApplyTo() runs it changes the IQueryable to returning an anonymous type, and there for blows up when the query is executed. So I tried returning an IEnumerable so the type wouldn't matter, but when I do that OData can't seem to handle that and starts spitting out 406 errors.

1

1 Answers

-1
votes

In your scenario, change your controller inherent form ApiController, and return

result.AsQueryable();

This may be a work around for $select.