C# 通用EF6存储库方法没有';无法生成正确的SQL
我的存储库中有这个方法公开了EF6 DbContextC# 通用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(
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>();
}