Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/310.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/linq/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何实现异步方法中的WHERE_C#_Linq_Asp.net Core - Fatal编程技术网

C# 如何实现异步方法中的WHERE

C# 如何实现异步方法中的WHERE,c#,linq,asp.net-core,C#,Linq,Asp.net Core,我正在尝试使用Where()实现一个异步方法。我找不到Where方法的任何异步版本。对于IEnumerable,ToListSync不可用。我应该使用不同的方法吗 public async Task<IEnumerable<TEntity>> Where(Func<TEntity, bool> predicate) { var tmp = await this.DbSet.Where(predicate).ToListAsync(); //error:

我正在尝试使用Where()实现一个异步方法。我找不到Where方法的任何异步版本。对于IEnumerable,ToListSync不可用。我应该使用不同的方法吗

public async Task<IEnumerable<TEntity>> Where(Func<TEntity, bool> predicate)
{
    var tmp = await this.DbSet.Where(predicate).ToListAsync(); //error: ToListAsync not available

    return tmp;
}
公共异步任务,其中(Func谓词)
{
var tmp=wait this.DbSet.Where(谓词).ToListAsync();//错误:ToListAsync不可用
返回tmp;
}
DbSet的类型为
DbSet

该项目以.NET标准2.0为目标,并引用了Microsoft.EntityFrameworkCore 2.2.1,我定义了“使用Microsoft.EntityFrameworkCore”

我在中看到,大多数扩展都以IQueryable为目标,但我使用的是IEnumerable。这可能是问题所在吗? 但是Where()返回IEnumerable


有什么想法吗?

您需要导入命名空间
Microsoft.EntityFrameworkCore
。有关详细信息,请参见官方网站。

您需要使用表达式,而不是Func

因此:

公共异步任务,其中(表达式谓词)
{
var tmp=wait this.DbSet.Where(谓词).ToListAsync();
返回tmp;
}
Func
需要实际函数,而
Expression
需要表达式树。
IQueryable
基于此表达式树构建SQL查询。 通过使用Func,可以从常规LINQ调用Where重载,该重载返回IEnumerable。
它没有ToListSync。

您必须添加其命名空间,该命名空间包含在
EntityFramework.dll


请查找。

@er mfahhgk您知道ASP.NET Core是否有类似的功能吗?我使用Microsoft.EntityFrameworkCore 2.2.1,并有“使用Microsoft.EntityFrameworkCore”
public async Task<IEnumerable<TEntity>> Where(Expression<Func<TEntity, bool>> predicate)
{
    var tmp = await this.DbSet.Where(predicate).ToListAsync();
    return tmp;
}