Home > Back-end >  Total page count and return type with asp.net core IQueryable
Total page count and return type with asp.net core IQueryable

Time:02-03

I implemented layered architecture on .net core. "core,repository,services" contains models and dto's. In the repository layer I receive the data and send it to the service layer.

But I want to send the number of pages as below. How can I do that ?

This code is located in the service layer. and this is how I return to the user. I have to go back like this

ServiceRepository the class I'm implementing the layer

     Task<CustomResponseDto<List<MaterialDemandDto>>> GetMaterialDemandList(int page, int pageSize);
    
    public async Task<CustomResponseDto<List<MaterialDemandDto>>> GetMaterialDemandList(int page, int pageSize)
    {
        int totalCount;
        var materialList = await _repository.GetMaterialDemandList(page, pageSize, out totalCount);
        var materialDemandsListDto = _mapper.Map<List<MaterialDemandDto>>(materialList);


        return CustomResponseDto<List<MaterialDemandDto>>.Success(200, materialDemandsListDto);
    }

This code is in the repository layer, and this is how I send the number of pages and the desired data to the repository layer.

But I want to send the totalCount here to the service layer. so i want to send into above code

Repository the class I'm implementing the layer

       Task<(List<MaterialDemand>, int)> GetMaterialDemandList(int page, int pageSize, out int totalCount);

        public async Task<(List<MaterialDemand>, int)> GetMaterialDemandList(int page, int pageSize, out int totalCount)
    {
        IQueryable<MaterialDemand> query;

        query = _context.MaterialDemands
                                    .Include(c => c.MaterialDemandDetails)
                                    .OrderByDescending(x => x.CreatedDate);
        int totalCount2 = query.Count();
        return (await query.Skip((pageSize * (page - 1))).Take(pageSize).ToListAsync(), totalCount2);
    }

I get this error

Async methods cannot have ref, in or out parameters DynamicManagemetn.Repository

CodePudding user response:

You can use tuple value types to return more than one value in a method. This is what you are looking for I guess and it is pretty easy to implement. You can do just like below:

 public async Task<(List<MaterialDemand>,int)> GetMaterialDemandList(int page, int pageSize)
 {

    IQueryable<MaterialDemand> query;

    query = _context.MaterialDemands
                                .Include(c => c.MaterialDemandDetails)
                                .OrderByDescending(x => x.CreatedDate);
   int totalCount = query.Count();  

   return (await query.Skip((pageSize * (page - 1))).Take(pageSize).ToListAsync(),totalCount );
 }

You can use many other ways like creating an object which contains TotalAccount property and MaterialDemand and return this on the method.

 public class MyCustomType
 {

    public int TotalAccount { get; set; }
    public List<MaterialDemand> MaterialDemands { get; set; }
 }

  public async Task<MyCustomType> GetMaterialDemandList(int page, int pageSize)
  {

        IQueryable<MaterialDemand> query = _context.MaterialDemands
                                        .Include(c => c.MaterialDemandDetails)
                                       .OrderByDescending(x => x.CreatedDate);

        return new MyCustomType
        {
            TotalAccount = query.Count(),
            MaterialDemands = await query.Skip((pageSize * (page - 1))).Take(pageSize).ToListAsync()

        };
    }
  •  Tags:  
  • Related