Entity Framework asynchronous record counter with records

I am trying to get records from a database, but in the same call DB gets the number of records. My methods are asynchronous, so I'm not sure if this is the correct way:

    public async Task<IEnumerable<ProductItemViewModel>> GetAsync(int pageIndex, int pageSize)
    {
        var products = this.Repository.GetAll<Product>().OrderBy(p => p.Price).AsQueryable();

        var count = await products.CountAsync();
        if (count == 0)
            return new List<ProductItemViewModel>();

        products = products.ToPaginatedList(pageIndex, pageSize);
        var result = await products.ToListAsync();

        var viewModels = new List<ProductItemViewModel>();
        Mapper.Map(result, viewModels);

        return viewModels.AsEnumerable();
    }

I think it's not so good to wait for CountAsync (), but I'm not sure how to implement it.

Sincerely.

EDIT:

I apologize for not returning the account somewhere, since now it looks like I am only calculating due to the == 0 check, but in the end I will return the account with the records, because I need it for my angular swap. I didn’t understand how I would be back (new vs tuple class), so I skipped it and focused on the Framework Count () entity and the entity in one DB call.

+4
2

, :

using AutoMapper.QueryableExtensions;
//...
public async Task<IEnumerable<ProductItemViewModel>> GetAsync(int pageIndex, int pageSize)
{
    var products = this.Repository.GetAll<Product>().OrderBy(p => p.Price);// I think you don't need to call AsQueryable(), you already are working with IQueryable
    var result=await products.Skip(pageIndex*pageSize).Take(pageSize).ProjectTo<ProductItemViewModel>().ToListAsync();
    return result;
}

:

public async Task<IEnumerable<ProductItemViewModel>> GetAsync(int pageIndex, int pageSize)
{

    return await this.Repository.GetAll<Product>()
                                .OrderBy(p => p.Price)
                                .Skip(pageIndex*pageSize)
                                .Take(pageSize)
                                .ProjectTo<ProductItemViewModel>()
                                .ToListAsync();
}

.ProjectTo<ProductItemViewModel>() , AutoMapper select IQueryable, Entity Framework , , , , IQueryable a ProductItemViewModel Select.

+2

, . , TotalResultCount . , .

CountAsync() results.Count . , , , .

public async Task<IEnumerable<ProductItemViewModel>> GetAsync(int pageIndex, int pageSize)
{
    var result = await this.Repository.GetAll<Product>()
        .OrderBy(p => p.Price)
        .ToPaginatedList(pageIndex, pageSize)
        .ToListAsync();

    if (result.Count == 0)
    {
        // This "if" really isn't necessary, as your mapper should map an
        // empty collection to an empty collection. But it is a minor
        // efficiency improvement, and it speaks to your original code.

        return Enumerable.Empty<ProductItemViewModel>();
    }

    var viewModels = new List<ProductItemViewModel>();
    Mapper.Map(result, viewModels);

    return viewModels; // AsEnumerable() has no effect here, as the cast will happen by
                       // default anyway
}

, . , , , , , , .


, ; .

, .

, Entity Framework T-SQL COUNT(*) OVER (), , .

, :

  • ?
  • x..y

, , , , . , , , Entity Framework .

, async, . await CountAsync() - , .

+1

Source: https://habr.com/ru/post/1622022/


All Articles