handling selects in odataqueryoptions in core?

Viewed 735

I've got a regular controller, that has odata capabilities:

[Route("[controller]")]
public class ChannelsController : Controller
{
    private readonly ChannelContext db = new ChannelContext();
    public IQueryable<PrdChannel> Get()
    {
        var entities = db.PrdChannel.AsQueryable();
        var modelManager = (IODataModelManger) HttpContext.RequestServices.GetService(typeof(IODataModelManger));
        var model = modelManager.GetModel(nameof(ProductConfiguration));
        var queryContext = new ODataQueryContext(model, typeof(PrdChannel), null);
        var queryOptions = new ODataQueryOptions(queryContext, HttpContext.Request);

        return queryOptions
            .ApplyTo(entities, new ODataQuerySettings
            {
                HandleNullPropagation = HandleNullPropagationOption.True
            })
            .Cast<PrdChannel>();
    }
}

I am able to issue a GET: https://myservice/Channel?$filter=cable lt 10

However, when I try to do a $select, it does not work!

https://myservice/Channel?$select=cable

Per the definition of OdataQueryOptions, it does not support select/expand.

Question: How do we support $select in aspnetcore?

Perhaps this should be an ODataController?

1 Answers

Duplicated: Can't apply $select when using ODataQueryOptions

To allow support for $select in OData, you cant Cast your IQueryable to a defined type, you need to use dynamic type instead.

    public IQueryable<dynamic> Get()
    {
        ... your code ...
        return queryOptions
            .ApplyTo(entities, new ODataQuerySettings
            {
                HandleNullPropagation = HandleNullPropagationOption.True
            })
            as IQueryable<dynamic>;
    }
Related