OData / OData/AspNetCoreOData

Unable to get $count in http://localhost:xxxxx/odata/users/$count

Open
#261 15 comments 0 reactions 1 assignee View on GitHub

@xuzhg is already working on this.

Since Jul 29, 2021.

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

Description

I am using Odata with .NET 5.0. Odata version is : (Microsoft.AspNetCore.OData\8.0.1)

http://localhost:xxxxx/odata/users/$count, is not returning $count value

What is the best approach to set end points of $count and get value with http://localhost:xxxxx/odata/users/$count

My ODataController and Startup.cs files are below.

using Comman.TestData;
using Domain;
using Domain.BO;
using Domain.Service;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.OData.Query;
using System;
using System.Collections.Generic;
using System.Linq;
using AspNetCoreOData.Helper;
using Microsoft.AspNetCore.OData.Results;
namespace AspNetCoreOData.Controllers
{
    // [ApiController]
    // [Route("[controller]")]
    public class UsersController : ControllerBase
    {
        private readonly IMapperSession session;

        // 0. Decalare constant - 2000
        private const int batchSize = 2000;

        public UsersController(IMapperSession session)
        {
            this.session = session;
        }

        [HttpGet]
        // [EnableQuery()]
        public PageResult<User> Get(ODataQueryOptions options)
        {
            long? count = null;
            // 1. Get all IQuerable<User> from dao. 
            var userList = session.Users;

            // 2. Define Include/ExcludeIDs more than 5000
            List<int> includeIdsList = TestData.GetIncludeExcludeIdList();

            IQueryable<User> results = Enumerable.Empty<User>().AsQueryable();

            for (int i = 0; i < includeIdsList.Count; i = i + batchSize)
            {
                // 3. Split Include/ ExcludeIds in batch(constant - options.count)
                var items = includeIdsList.Skip(i).Take(batchSize).ToArray();

                IQueryable<User> getGridData = IQuerableHelper.GetGridData(userList, items, null);

                if (options.Filter != null)
                {
                    // 4.Apply Filter with Include / ExcludeIds in loop of batch.count and total includeId/ ExcludeId
                    results = results.Concat((IQueryable<User>)options.Filter.ApplyTo(getGridData, new ODataQuerySettings()));
                }
                else
                {
                    results = results.Concat(getGridData);
                }
            }

            if (options.OrderBy != null)
            {
                results = options.OrderBy.ApplyTo(results);  //perform sort 
            }

            if (options.Count != null && options.Count.Value)
            {
                count = options.Count.GetEntityCount(results).Value;
            }

            if (options.Skip != null)
            {
                results = options.Skip.ApplyTo(results, new ODataQuerySettings());  //perform skip 
            }

            if (options.Top != null)
            {
                results = options.Top.ApplyTo(results, new ODataQuerySettings());  //perform take 
            }

            return (new PageResult<User>(results, null, count));

        }
    }
}

using Domain.BO;
using Domain.NH;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.OData;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.OData.Edm;
using Microsoft.OData.ModelBuilder;
using Microsoft.OpenApi.Models;

namespace AspNetCoreOData
{
    public class Startup
    {
        public Startup(IConfiguration configuration)
        {
            Configuration = configuration;
        }

        public IConfiguration Configuration { get; }

        // This method gets called by the runtime. Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services)
        {
            var connStr = Configuration.GetConnectionString("DefaultConnection");

            services.AddNHibernate(connStr);

            services.AddControllers()
                .AddOData(
                 opt => opt.Select()
                .SkipToken()
                .SetMaxTop(null)
                .Count()
                .Filter()
                .AddRouteComponents("odata", GetEdmModel()));


            services.AddSwaggerGen(c =>
            {
                c.SwaggerDoc("v1", new OpenApiInfo { Title = "AspNetCoreOData", Version = "v1" });
            });

            // services.AddControllersWithViews();

        }

        IEdmModel GetEdmModel()
        {
            var odataBuilder = new ODataConventionModelBuilder();

            odataBuilder.EntitySet<User>("Users");

            odataBuilder.EntitySet<User>("Employee");

            odataBuilder.EntitySet<WeatherForecast>(nameof(WeatherForecast));

            return odataBuilder.GetEdmModel();
        }

        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
        {
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
                app.UseSwagger();
                app.UseSwaggerUI(c => c.SwaggerEndpoint("/swagger/v1/swagger.json", "AspNetCoreOData v1"));
            }

            app.UseRouting();

            app.UseAuthorization();

            app.UseEndpoints(endpoints =>
            {
                endpoints.MapControllers();

            });

        }
    }
}

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.