Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/307.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/7/sql-server/27.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# 通用EF6存储库方法没有';无法生成正确的SQL_C#_Sql Server_Entity Framework - Fatal编程技术网

C# 通用EF6存储库方法没有';无法生成正确的SQL

C# 通用EF6存储库方法没有';无法生成正确的SQL,c#,sql-server,entity-framework,C#,Sql Server,Entity Framework,我的存储库中有这个方法公开了EF6 DbContext public IList<T> GetEntities<T>(Func<T, bool> predicate) where T : class { return db.Set<T>().Where(predicate).ToList<T>(); } public IList GetEntities(Func谓词),其中T:class { 返回db.Set().Where(

我的存储库中有这个方法公开了EF6 DbContext

public IList<T> GetEntities<T>(Func<T, bool> predicate) where T : class
{
    return db.Set<T>().Where(predicate).ToList<T>();
}
public IList GetEntities(Func谓词),其中T:class
{
返回db.Set().Where(谓词).ToList();
}
当我看到这个方法在SQL Profiler中执行时,谓词在内存中执行。SQL语句不包含where子句


有什么想法吗?

。其中
接受两件事中的一件,要么是
Func
,要么是
Expression
。如果传入
表达式
,则EF查询应能正常工作

public IList<T> GetEntities<T>(Expression<Func<T, bool>> predicate) where T : class

当您传入
Func
时,执行
IEnumerable
实现,它使用Linq到对象而不是Linq到实体。

您的谓词应该是一个
表达式
,以便实体框架可以实际使用它来生成SQL,而不仅仅是执行它。如果传入一个
Func
,则实际上是在调用该方法,而不是:

public IList GetEntities(表达式谓词),其中T:class
{
返回db.Set().Where(谓词).ToList();
}

谢谢!现在效果很好。
GetEntities(x => x.Id == 34)
public IList<T> GetEntities<T>(Expression<Func<T, bool>> predicate) where T : class
{
    return db.Set<T>().Where(predicate).ToList<T>();
}