OData / OData/AspNetCoreOData

How to get IQueryable<TEntity> after applying queryOptions.SelectExpand?

Open
#333 7 comments 0 reactions 1 assignee View on GitHub

@xuzhg is already working on this.

Since Oct 19, 2021.

question
Dominant language
C#
Stars
505
Forks
186
PR merge metrics
No merged PRs in 30d

Description

I am trying to apply the OData filters, selects, expands, orderbys to an IQueryable object.

Here is what I have

        [HttpGet("first")]
        public async Task<ActionResult<TModel>> FirstAsync(
            [FromQuery(Name = "$orderby")] string orderby = null,
            [FromQuery(Name = "$filter")] string filter = null,
            [FromQuery(Name = "$select")] string select = null,
            [FromQuery(Name = "$expand")] string expand = null)
        {
            var settings = new ODataValidationSettings()
            {
                AllowedFunctions = AllowedFunctions.AllFunctions,
                AllowedQueryOptions = AllowedQueryOptions.All,
                MaxOrderByNodeCount = 4,
                MaxTop = 1,
            };
            // I have an extension that reads the request and creates the ODataQueryOptions<TModel>
            ODataQueryOptions<TModel> queryOptions = Request.GetODataQueryOptions<TModel>();
        
            queryOptions.Validate(settings);

            IQueryable<TModel> query = BuildQuery(queryOptions, new ODataQuerySettings());

             return Ok(await query.FirstOrDefaultAsync());
        }

          [HttpGet]
          public async Task<ActionResult<TModel[]>> QueryAsync (
           [FromQuery(Name = "$orderby")] string orderby = null,
           [FromQuery(Name = "$filter")] string filter = null,
           [FromQuery(Name = "$select")] string select = null,
           [FromQuery(Name = "$expand")] string expand = null)
        {
            var settings = new ODataValidationSettings()
            {
                AllowedFunctions = AllowedFunctions.AllFunctions,
                AllowedQueryOptions = AllowedQueryOptions.All,
                MaxTop = SettingsProvider.DefaultMaxRecordsPerPage,
                MaxOrderByNodeCount = 4,
            };

            var queryOptions = Request.GetODataQueryOptions<TModel>();

            queryOptions.Validate(settings);

            IQueryable<TModel> query = BuildQuery(queryOptions, new ODataQuerySettings());

            var models = await query.ToArrayAsync();

            return Ok(models);
        }
        protected IQueryable<TModel> BuildQuery(ODataQueryOptions<TModel> queryOptions, ODataQuerySettings settings)
        {
            IQueryable<TModel> query = Repository;

            if (queryOptions.SelectExpand != null)
            {
             
                var queryable = queryOptions.SelectExpand.ApplyTo(query, settings);
                // this does not work
                query = queryable.Cast<TModel>();
            }

            if (queryOptions.Filter != null)
            {
                query = queryOptions.Filter.ApplyTo(query, settings) as IQueryable<TModel>;
            }

            if (queryOptions.OrderBy != null)
            {
                query = queryOptions.OrderBy.ApplyTo(query);
            }

            return query;
        }

The line queryable.Cast<TModel>(); causes the following error.

No coercion operator is defined between types

Microsoft.AspNetCore.OData.Query.Wrapper.SelectAllAndExpand`1[.Models.Survey]' and 'Models.Survey'

Is this an issue? if not, how is the right apply the filters to IQueryable?

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.