4

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?

Alex Gordon
  • 57,446
  • 287
  • 670
  • 1,062
  • Does this answer your question? [Can't apply $select when using ODataQueryOptions](https://stackoverflow.com/questions/55636167/cant-apply-select-when-using-odataqueryoptions) – Rodrigo G Rodrigues Oct 28 '20 at 17:04

1 Answers1

0

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>;
    }
Rodrigo G Rodrigues
  • 331
  • 1
  • 3
  • 16