【问题标题】:Total page count and return type with asp.net core IQueryable使用 asp.net core IQueryable 的总页数和返回类型
【发布时间】:2022-02-02 19:05:50
【问题描述】:

我在 .net 核心上实现了分层架构。 “核心、存储库、服务”包含模型和 dto。 在存储层中,我接收数据并将其发送到服务层。

但我想发送如下页数。我该怎么做?

此代码位于服务层。这就是我返回给用户的方式。我必须这样回去

ServiceRepository 我正在实现层的类

     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);
    }

此代码位于存储库层中,这就是我将页数和所需数据发送到存储库层的方式。

但我想将此处的 totalCount 发送到服务层。所以我想发送到上面的代码

存储库 我正在实现层的类

       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);
    }

我收到此错误

异步方法不能有 ref、in 或 out 参数 DynamicManagemetn.Repository

【问题讨论】:

    标签: asp.net-core asp.net-web-api


    【解决方案1】:

    您可以使用tuple 值类型在一个方法中返回多个值。我猜这就是你要找的东西,而且很容易实现。 你可以像下面这样:

     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 );
     }
    

    您可以使用许多其他方法,例如创建一个包含 TotalAccount 属性和 MaterialDemand 的对象,并将其返回到方法中。

     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()
    
            };
        }
    

    【讨论】:

    • 嗨,我照你说的做了,但它给出了如下错误异步方法不能有引用、输入或输出参数 DynamicManagemetn.Repository
    • @Fatihmzm 但我与您分享的方法中没有任何输出参数。你不能在异步方法中有 out 或 ref 。只需从方法参数中删除此部分即可。
    猜你喜欢
    • 2020-05-12
    • 2011-07-03
    • 2018-02-01
    • 2013-02-23
    • 1970-01-01
    • 1970-01-01
    • 2020-02-09
    • 2019-03-12
    • 1970-01-01
    相关资源
    最近更新 更多