Joh*_* DK 2 .net c# linq async-await .net-core
目前,我的数据对象存储库模式之一具有以下方法:
public async Task<IEnumerable<DropDownList>> GetDropDownListNoTracking()
{
return await context.TouchTypes
.AsNoTracking()
.Where(s => s.IsActive)
.Select(s => new DropDownList()
{
Id = s.Id,
Name = s.Description
}).ToListAsync();
}
Run Code Online (Sandbox Code Playgroud)
当我在页面视图中调用它时:
private IList<DropDownList> TouchTypeList { get; set; }
private async Task LoadDropDownAsync()
{
TouchTypeList = await _unitOfWork.TouchType.GetDropDownListNoTracking();
}
Run Code Online (Sandbox Code Playgroud)
我试图理解为什么我不能只做 aGetDropDownListNoTracking().ToList()
而是它希望我投射 : (IList<DropDownList>)。
我可以轻松地更改属性来解决此问题,但我认为.ToList可以在这里工作吗?
我主要只是想理解这一点,以便我能以正确的方式做到这一点。
GetDropDownListNoTracking返回Task<IEnumerable<DropDownList>>,而不是IEnumerable<DropDownList>,所以你必须这样做:
private async Task LoadDropDownAsync()
{
TouchTypeList = (await _unitOfWork.TouchType.GetDropDownListNoTracking()).ToList();
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
262 次 |
| 最近记录: |