I am currently developing an Api. Where I have a GET endpoint. where I am using OData Query attribute i.e. [EnableQuery] .
I have added the filter in the Program.cs . Its in .Net 6
builder.Services.AddControllers(options =>
{
options.Filters.Add(new EnableQueryAttribute()
{
AllowedQueryOptions = AllowedQueryOptions.All,
AllowedOrderByProperties = null,
AllowedLogicalOperators = AllowedLogicalOperators.All,
});
}).AddOData(o => o.Select().Filter().SetMaxTop(100).OrderBy().Count());
So issue is, while querying to database, Odata filter not getting applied, instead it etch all records then applying the filter. So in the real scenario where there is huge record count its a bottleneck situation. How to implement the filters before hitting to the database? I am using Iqueryable.
In the repository layer i am doing as below
public async Task<IEnumerable<T>> GetItemsAsync(CancellationToken cancellationToken)
{
var queryable1 = _samplecontainer.GetItemLinqQueryable<abc>();
var queryable2 = queryable1.Where(_ => _.sampleType== "generic").ToFeedIterator();
List<T> results = new List<T>();
while (queryable2.HasMoreResults)
{
FeedResponse<T> response = await queryable2.ReadNextAsync();
results.AddRange(response.ToList());
}
return results; // Here it returns all data.
}
What I am missing here?