我的存储库中有此方法公开 EF6 DbContext。
public IList<T> GetEntities<T>(Func<T, bool> predicate) where T : class
{
return db.Set<T>().Where(predicate).ToList<T>();
}
当我在 SQL Profiler 中观察此方法执行时,谓词在内存中执行。 SQL语句不包含where子句。
有什么想法吗?
最佳答案
.Where
接受两件事之一,要么 Func<T, bool>
或Expression<Func<T, bool>>
。如果你传入Expression<Func<T, bool>>
,那么您的 EF 查询应该可以正常工作。
public IList<T> GetEntities<T>(Expression<Func<T, bool>> predicate) where T : class
你可以用同样的方式调用它:
GetEntities(x => x.Id == 34)
当你传入Func<T, bool>
时,IEnumerable<T>
执行执行,它使用 Linq-to-Objects 而不是 Linq-to-Entities。
关于c# - 通用 EF6 存储库方法不会生成正确的 SQL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38857007/