且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何投射 DbSet<T>列出<T>

更新时间:2022-04-02 21:01:33

IDBSet 继承自 IQueryable, IEnumerable, IQueryableIEnumerable,因此您不能以这种方式直接将其转换为列表.您可以通过使用 .ToList().ToListAsync() 获得 DBSet 中所有实体的 List

IDBSet inherits from IQueryable<TEntity>, IEnumerable<TEntity>, IQueryable, and IEnumerable, so you can't directly cast it to a list that way. You could get a List<TEntity> of all entities in the DBSet though by using .ToList() or .ToListAsync()

这会创建内存中所有实体的副本,因此您应该考虑直接在 DBSet 上使用 LINQ 进行操作

THis creates a copy of all entities in memory though, so you should consider operating with LINQ directly on the DBSet