小编Joh*_* DK的帖子

试图理解我的 IEnumerable 以及为什么我不能使用 .ToList()

目前,我的数据对象存储库模式之一具有以下方法:

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可以在这里工作吗?

我主要只是想理解这一点,以便我能以正确的方式做到这一点。

.net c# linq async-await .net-core

2
推荐指数
1
解决办法
262
查看次数

标签 统计

.net ×1

.net-core ×1

async-await ×1

c# ×1

linq ×1