使用 WhereIf 时找不到 .ToListAsync()
.ToListAsync() not found when using WhereIf
我正在阅读this tutorial。我想在 EF Core 中使用 async
查询。
当我这样使用时效果很好:
var tasks = await _taskRepository
.GetAll()
//.WhereIf(!string.IsNullOrEmpty(input?.Title), x => x.Title.Contains(input.Title))
//.WhereIf(input?.State != null, x => x.State == input.State.Value)
//.OrderByDescending(x => x.CreationTime)
.ToListAsync();
但我想像这样使用 whereif 和 orderby
var tasks = await _taskRepository
.GetAll()
.WhereIf(!string.IsNullOrEmpty(input?.Title), x => x.Title.Contains(input.Title))
.WhereIf(input?.State != null, x => x.State == input.State.Value)
.OrderByDescending(x => x.CreationTime)
.ToListAsync();
错误:
'IOrderedEnumerable' does not contain a definition for 'ToListAsync' and no extension method 'ToListAsync' accepting a first argument of type 'IOrderedEnumerable' could be found (are you missing a using directive or an assembly reference?)
IOrderedEnumerable<Task>
表示您正在使用 IEnumerable<Task>
。
Entity Framework Core 使用 IQueryable<T>
(代表数据库查询),而不是 IEnumerable<T>
(代表内存中的集合)。一旦 IQueryable<T>
转换为 IEnumerable<T>
,就会在数据库服务器上执行查询并检索结果。
因此:如果您正在调用 returns IEnumerable<T>
的方法,则该方法不能用于 LINQ to Entities。
您使用了错误的 WhereIf 扩展名,很容易错过,因为您需要添加额外的使用 Visual Studio 不会提供。
您正在使用 returns IEnumerable
的扩展
Abp.Collections.Extensions.EnumerableExtensions.WhereIf<T>()
您需要使用 returns IQueryable
的扩展
Abp.Linq.Extensions.QueryableExtensions.WhereIf<T>()
修复很简单,只需在文件顶部添加 using Abp.Linq.Extensions;
我正在阅读this tutorial。我想在 EF Core 中使用 async
查询。
当我这样使用时效果很好:
var tasks = await _taskRepository
.GetAll()
//.WhereIf(!string.IsNullOrEmpty(input?.Title), x => x.Title.Contains(input.Title))
//.WhereIf(input?.State != null, x => x.State == input.State.Value)
//.OrderByDescending(x => x.CreationTime)
.ToListAsync();
但我想像这样使用 whereif 和 orderby
var tasks = await _taskRepository
.GetAll()
.WhereIf(!string.IsNullOrEmpty(input?.Title), x => x.Title.Contains(input.Title))
.WhereIf(input?.State != null, x => x.State == input.State.Value)
.OrderByDescending(x => x.CreationTime)
.ToListAsync();
错误:
'IOrderedEnumerable' does not contain a definition for 'ToListAsync' and no extension method 'ToListAsync' accepting a first argument of type 'IOrderedEnumerable' could be found (are you missing a using directive or an assembly reference?)
IOrderedEnumerable<Task>
表示您正在使用 IEnumerable<Task>
。
Entity Framework Core 使用 IQueryable<T>
(代表数据库查询),而不是 IEnumerable<T>
(代表内存中的集合)。一旦 IQueryable<T>
转换为 IEnumerable<T>
,就会在数据库服务器上执行查询并检索结果。
因此:如果您正在调用 returns IEnumerable<T>
的方法,则该方法不能用于 LINQ to Entities。
您使用了错误的 WhereIf 扩展名,很容易错过,因为您需要添加额外的使用 Visual Studio 不会提供。
您正在使用 returns IEnumerable
的扩展Abp.Collections.Extensions.EnumerableExtensions.WhereIf<T>()
您需要使用 returns IQueryable
的扩展Abp.Linq.Extensions.QueryableExtensions.WhereIf<T>()
修复很简单,只需在文件顶部添加 using Abp.Linq.Extensions;