所以我有一个具有定义的属性EFCore.DBSet<AccessTime> AccessTime
的上下文类EFCoreMContext
public class EFCoreMContext : Microsoft.EntityFrameworkCore.DbContext
{
public Microsoft.EntityFrameworkCore.DbSet<User> Users { get; set; }
public Microsoft.EntityFrameworkCore.DbSet<AccessTime> AccessTime { get; set; }
...
}
然后我有一个存储库类CAccessTimeRepository
public class CAccessTimeRepository : IRepository<AccessTime>
{
private EFCoreMContext db;
public CAccessTimeRepository(EFCoreMContext context)
{
this.db = context;
}
...
使用异步功能:
public async Task<IEnumerable<AccessTime>> FindAsync(Func<AccessTime, bool> predicate)
{
return await db.AccessTime.Where(predicate).ToListAsync();
}
...
}
问题是,当我try 使用这个ToListAsync()
时-Visual Studio突出显示了它,但出现错误
CS1061 IEnumerable不包含ToListAsync()
问题是,当我用.Where()
英镑买这样的东西时
.Where(x => x.ID == 1)
它返回IQueryable
,所以我可以使用ToListAsync()
.但是,当我将此方法与我的谓词一起使用时,它返回IEnumerable
.
所以我试着用AsQueryable
来传递这个代码
public async Task<IEnumerable<AccessTime>> FindAsync(Func<AccessTime, bool> predicate)
{
return await db.AccessTime
.Where(predicate)
.AsQueryable()
.ToListAsync();
}
在执行此代码时,我收到一个错误:
源‘Iqueryable’不实现‘IAsyncEnumerable<;_3TierApp.DAL.Entities.AccessTime>;’.只有实现了"IAsyncEnumerable"的源才能用于实体框架异步操作.
我不知道为什么带谓词的.Where()
和with x => x.Id
返回不同的类型.
有谁能解释一下吗?还有,对此最好的解决方案是什么?