【问题标题】:How can I use ToListAsync() method with ABP repository?如何在 ABP 存储库中使用 ToListAsync() 方法?
【发布时间】:2020-04-08 20:26:05
【问题描述】:

我有一个应用程序服务方法GetWithName,我想编写它的异步版本。为此,我尝试使用ToListAsync(),但存储库没有此方法。

public PagedResultDto<BookDto> GetWithName(SearchWithNameRequestDto input)
{
    var books = 
        Repository
        .Where(p => p.Name.Contains(input.Name))
        .ToList();

    return new PagedResultDto<BookDto>
    {
        TotalCount = books.Count,
        Items = ObjectMapper.Map<List<Book>, List<BookDto>>(books)
    };
}

如何使用异步版本的ToList 方法?

【问题讨论】:

    标签: c# asynchronous iqueryable abp


    【解决方案1】:

    ToListAsync 依赖于 ORM:

    // using Microsoft.EntityFrameworkCore;
    
    var query = Repository
        .Where(p => p.Name.Contains(input.Name));
    var books = await query.ToListAsync();
    

    对于 DI(独立于 ORM)解决方案,注入 IAsyncQueryableExecuter 并执行以下操作:

    // using Abp.Linq;
    
    var query = Repository
        .Where(p => p.Name.Contains(input.Name));
    var books = await _asyncQueryableExecuter.ToListAsync(query);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-02-09
      • 2012-11-28
      • 2019-10-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-09-01
      • 1970-01-01
      相关资源
      最近更新 更多