Mur*_*lex 1 c# expression entity-framework
考虑这两个代码:
运用 Func<T, bool>
public IQueryable<Blog> GetBlogs(Func<Blog, bool> predicate)
{
return context.Blogs.Where(predicate).AsQueryable();
}
Run Code Online (Sandbox Code Playgroud)
运用 Expression<Func<T, bool>>
public IQueryable<Blog> GetBlogs(Expression<Func<Blog, bool>> predicate)
{
return context.Blogs.Where(predicate); // No need of AsQueryable
}
Run Code Online (Sandbox Code Playgroud)
因此,在第一种情况下,实体框架将始终返回数据库中的所有对象,对吧?那么打电话有AsQueryable什么意义呢?无论如何它有帮助吗?它与Expression版本相似?
无论如何它有帮助吗?
没有.
所有这一切都取决于方法的调用者,因为他们认为他们有一个IQueryable会将应用于它的任何其他操作符转换为在数据库中运行的SQL,而实际上你只有一件IEnumerable穿着羊皮的衣服.如果你真的希望在内存中执行操作,而不是在数据库中执行,那么至少要明确它,并将其IEnumerable键入为IEnumerable.