How to get IQueryable<TEntity> after applying queryOptions.SelectExpand?
Open
@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
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Assessment
This issue has not been assessed yet.