Base solution for your next web application
Ends in:
01 DAYS
01 HRS
01 MIN
01 SEC
Open Closed

add async to GetProvinces method have Error #4410


User avatar
0
ramezani583 created

hi, i can not change GetProvinces(same as getpeople method in step by step developing) to async!

when i changing "private readonly IRepository<Province.Province> _provinceRepository" to "private readonly IRepository<Province> _provinceRepository" i have error in build

and when changing "private readonly IRepository<Province> _provinceRepository" to "private readonly IRepository<Province.Province> _provinceRepository"

GetProvinces method have error!

--

List<Province>' does not contain a definition for 'GetAwaiter' and no extension method 'GetAwaiter' accepting a first argument of type 'List<Province>' could be found (are you missing a using directive or an assembly reference?)

public interface IProvinceAppService : IApplicationService { Task <ListResultDto<ProvinceListDto>> GetProvinces(GetProvincesInput input); Task CreateProvince(CreateProvinceInput input); Task DeleteProvince(EntityDto input); }


namespace SSoft.ResourceAssessmentsNetwork.BasicInfo.Provinces { [AbpAuthorize(AppPermissions.Pages_BasicInfo_Province)] public class ProvinceAppService : ResourceAssessmentsNetworkAppServiceBase, IProvinceAppService {

    private readonly IRepository&lt;Province.Province&gt; _provinceRepository;

    public ProvinceAppService(IRepository&lt;Province.Province&gt; provinceRepository)
    {
        _provinceRepository = provinceRepository;
    }
  
    [AbpAuthorize(AppPermissions.Pages_BasicInfo_Province_CreateProvince)]
    public async Task CreateProvince(CreateProvinceInput input)
    {
        var province = ObjectMapper.Map&lt;Province.Province&gt;(input);
        await _provinceRepository.InsertAsync(province);
    }
    [AbpAuthorize(AppPermissions.Pages_BasicInfo_Province_DeleteProvince)]
    public async Task DeleteProvince(EntityDto input)
    {
        await _provinceRepository.DeleteAsync(input.Id);
    }
  
    public async Task&lt;ListResultDto&lt;ProvinceListDto&gt;> GetProvinces(GetProvincesInput input)
    {
       var provinces = await _provinceRepository
            .GetAll()
            .WhereIf(
                !input.Filter.IsNullOrEmpty(),
                p => p.Name.Contains(input.Filter) 
            )
            .OrderBy(p => p.Name)
            .ToList();
        return new ListResultDto&lt;ProvinceListDto&gt;(provinces.MapTo&lt;List&lt;ProvinceListDto&gt;>());
    }
}

}


10 Answer(s)
  • User Avatar
    0
    aaron created
    Support Team
    public async Task<ListResultDto<ProvinceListDto>> GetProvinces(GetProvincesInput input)
    {
       var provinces = await _provinceRepository
           .GetAll()
           .WhereIf(
               !input.Filter.IsNullOrEmpty(),
               p => p.Name.Contains(input.Filter)
           )
           .OrderBy(p => p.Name)
           .ToList();
       return new ListResultDto<ProvinceListDto>(provinces.MapTo<List<ProvinceListDto>>());
    }
    

    ToList does not return a Task, so it cannot be awaited. You can inject IAsyncQueryableExecuter and do:

    public async Task<ListResultDto<ProvinceListDto>> GetProvinces(GetProvincesInput input)
    {
        var query = _provinceRepository
            .GetAll()
            .WhereIf(
                !input.Filter.IsNullOrEmpty(),
                p => p.Name.Contains(input.Filter)
            )
            .OrderBy(p => p.Name);
        var provinces = await AsyncQueryableExecuter.ToListAsync(query); // This line
        return new ListResultDto<ProvinceListDto>(provinces.MapTo<List<ProvinceListDto>>());
    }
    

    Tip: Wrap your code in the following for formatting and readability:

    [code]
    

    [/code:28rmc8yk] A shortcut is to highlight your code and click on the </> button in the formatting toolbar.

  • User Avatar
    0
    ramezani583 created

    How to inject IAsyncQueryableExecuter and do?

  • User Avatar
    0
    aaron created
    Support Team

    You can property-inject, like this:

    public class ProvinceAppService : ResourceAssessmentsNetworkAppServiceBase, IProvinceAppService
    {
        public IAsyncQueryableExecuter AsyncQueryableExecuter { get; set; }
    
        public ProvinceAppService(IRepository<Province.Province> provinceRepository)
        {
            // ...
            AsyncQueryableExecuter = NullAsyncQueryableExecuter.Instance;
        }
    }
    

    My previous post already shows the do part.

  • User Avatar
    0
    ramezani583 created

    hi, i want get all record count for Grid Paging like this command " var provinceCount = await query.CountAsync();" but when write method

    public class ProvinceAppService : ResourceAssessmentsNetworkAppServiceBase, IProvinceAppService
        {
     public IAsyncQueryableExecuter AsyncQueryableExecuter { get; set; }
            private readonly IRepository<Province> _provinceRepository;
    
            public ProvinceAppService(IRepository<Province> provinceRepository)
            {
                _provinceRepository = provinceRepository;
                AsyncQueryableExecuter = NullAsyncQueryableExecuter.Instance;
    
            }
    public async Task<PagedResultDto<ProvinceListDto>> GetProvinces(GetProvincesInput input)
            {
                var query = _provinceRepository
                  **<span style="color:#BF0000">  .WhereIf(</span>**
                        !input.Filter.IsNullOrWhiteSpace(),
                        p =>
                            p.Name.Contains(input.Filter)
                    );
                var provinceCount = await query.CountAsync();
    
                var provinces = await query
                    .OrderBy(input.Sorting)
                    .PageBy(input)
                    .ToListAsync();
    
                var provinceListDtos = ObjectMapper.Map<List<ProvinceListDto>>(provinces);
    
                return new PagedResultDto<ProvinceListDto>(
                    provinceCount,
                    provinceListDtos
                    );
            }
    {
    

    I have error in ".WhereIf" <ins>'IRepository<Province>' does not contain a definition for 'WhereIf' and no extension method 'WhereIf' accepting a first argument of type 'IRepository<Province>' could be found (are you missing a using directive or an assembly reference?)</ins>

    I forced to write this method as follows:

    public class ProvinceAppService : ResourceAssessmentsNetworkAppServiceBase, IProvinceAppService
        {
     public IAsyncQueryableExecuter AsyncQueryableExecuter { get; set; }
            private readonly IRepository<Province> _provinceRepository;
    
            public ProvinceAppService(IRepository<Province> provinceRepository)
            {
                _provinceRepository = provinceRepository;
                AsyncQueryableExecuter = NullAsyncQueryableExecuter.Instance;
    
            }
    public async Task<PagedResultDto<ProvinceListDto>> GetProvinces(GetProvincesInput input)
            {
                var query = _provinceRepository
                    .GetAll()
                    .WhereIf(
                        !input.Filter.IsNullOrWhiteSpace(),
                        p =>
                            p.Name.Contains(input.Filter)
                    );
                var provinceCount = await query.CountAsync();
    
                var query1 = _provinceRepository
                    .GetAll()
                    .WhereIf(
                        !input.Filter.IsNullOrWhiteSpace(),
                        p =>
                            p.Name.Contains(input.Filter)
                    )
                    .OrderBy(p => input.Sorting)
                    .PageBy(input);
                var provinces = await AsyncQueryableExecuter.ToListAsync(query1);
    
    
                var provinceListDtos = ObjectMapper.Map<List<ProvinceListDto>>(provinces);
    
                return new PagedResultDto<ProvinceListDto>(
                    provinceCount,
                    provinceListDtos
                    );
            }
    }
    

    How can I fix this problem? Is there a better way?

  • User Avatar
    0
    aaron created
    Support Team

    There is no problem.

    GetAll() simply returns Table.AsQueryable();

  • User Avatar
    0
    ramezani583 created

    but i have compile Error in this way:

    public async Task<PagedResultDto<ProvinceListDto>> GetProvinces(GetProvincesInput input)
            {
                var query = _provinceRepository
                 .WhereIf
                        !input.Filter.IsNullOrWhiteSpace(),
                        p =>
                            p.Name.Contains(input.Filter)
                    );
                var provinceCount = await query.CountAsync();
    
                var provinces = await query
                    .OrderBy(input.Sorting)
                    .PageBy(input)
                    .ToListAsync();
    
                var provinceListDtos = ObjectMapper.Map<List<ProvinceListDto>>(provinces);
    
                return new PagedResultDto<ProvinceListDto>(
                    provinceCount,
                    provinceListDtos
                    );
            }
    

  • User Avatar
    0
    aaron created
    Support Team

    Why would you remove GetAll(), when you know it works and it's the correct way?

  • User Avatar
    0
    ramezani583 created

    when i use .getall(), a new error occurs!

  • User Avatar
    0
    aaron created
    Support Team

    Add: using System.Linq.Dynamic.Core;

  • User Avatar
    0
    ramezani583 created

    THANK YOU VERY MUCH.