[英]What's the better way to implement a condition at NHibernate query?
I am using a generics implementation of Query from NHibernate. 我正在使用NHibernate的Query的泛型实现。
My method: 我的方法:
public IEnumerable<TEntidade> ObterEntidadesPor(Func<TEntidade, bool> where) { return SessionNH.Query<TEntidade>().Where(where); }
In this case, the NHibernate first do the "select * from TEntidade" bring all information to memory after all this, he implement the "where" conditional. 在这种情况下,NHibernate首先执行“ select * from TEntidade”,将所有信息存储到内存中,然后执行“ where”条件。 This are taking to much time.
这需要很多时间。
Is there a better way to do it? 有更好的方法吗?
The problem was solved with Expression<func>
. 该问题通过
Expression<func>
解决。
public IEnumerable<TEntidade> ObterEntidadesPor(Expression<Func<TEntidade, bool>> where)
{
return SessionNH.Query<TEntidade>().Where(where);
}
While searching for answer, a colleague told me that the Func
executes the query before build an expression. 在寻找答案时,一位同事告诉我
Func
在构建表达式之前先执行查询。 To build an expression before executing the query, we have to use Expression
. 要在执行查询之前构建表达式,我们必须使用
Expression
。
This is also mentioned by @RomanArtiukhin in comments on question. @RomanArtiukhin在有关问题的评论中也提到了这一点。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.