使用NHibernate.Linq并获得2个查询以进行简单的选择,为什么?
下面是省略了不相关位的代码:使用NHibernate.Linq并获得2个查询以进行简单的选择,为什么?,linq,nhibernate,linq-to-nhibernate,Linq,Nhibernate,Linq To Nhibernate,下面是省略了不相关位的代码: public IEnumerable<T> GetByQuery(Expression<Func<T, bool>> filter { try { return Session.Linq<T>().Where(filter); } catch(Exception ex) { // custom exception handling here
public IEnumerable<T> GetByQuery(Expression<Func<T, bool>> filter
{
try
{
return Session.Linq<T>().Where(filter);
}
catch(Exception ex)
{
// custom exception handling here
}
finally
{
CloseSession();
}
return null;
}
public IEnumerable GetByQuery(表达式过滤器)
{
尝试
{
返回Session.Linq().Where(过滤器);
}
捕获(例外情况除外)
{
//这里的自定义异常处理
}
最后
{
CloseSession();
}
返回null;
}
它被称为的一个例子如下:
IEnumerabl<ClientReport> clientReports =
clientReportRepository.GetByQuery(item => item.ClientId = id);
IEnumerabl客户端端口=
clientReportRepository.GetByQuery(item=>item.ClientId=id);
正如您所看到的,没有什么奇怪的,并且以这种方式调用,我们正在访问数据库中的一个表,与任何其他表都没有关系。
有什么想法吗?
谢谢
clientReports
可能会在每次枚举时执行查询(例如,获取Count()
)
为了避免这种情况,请在作业中使用
.ToList()
。您能确认使用NHProf实际执行了两个查询吗?我让dba监视数据库,并执行了两个查询。似乎在调用GetByQuery方法时运行一个查询,然后在Diego下面提到的枚举查询时再次运行一个查询。