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<Province.Province> _provinceRepository;
public ProvinceAppService(IRepository<Province.Province> provinceRepository)
{
_provinceRepository = provinceRepository;
}
[AbpAuthorize(AppPermissions.Pages_BasicInfo_Province_CreateProvince)]
public async Task CreateProvince(CreateProvinceInput input)
{
var province = ObjectMapper.Map<Province.Province>(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<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>>());
}
}
}
10 Answer(s)
-
0
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.
-
0
How to inject IAsyncQueryableExecuter and do?
-
0
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.
-
0
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?
-
0
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 ); }
-
0
Why would you remove GetAll(), when you know it works and it's the correct way?
-
0
-
0
Add: using System.Linq.Dynamic.Core;
-
0
THANK YOU VERY MUCH.