使用NHibernate.Linq获取2个简单选择的查询,为什么?

时间:2010-08-05 17:24:34

标签: 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
    }
    finally
    {
        CloseSession();
    }
    return null;
}

并且被调用的示例如下所示:

IEnumerabl<ClientReport> clientReports = 
clientReportRepository.GetByQuery(item => item.ClientId = id);

因为你可以看到,没有任何花哨和以这种方式被调用,我们在数据库中命中一个表而没有与任何其他表的关系。但是当我在配置中有show_sql = true时,它显示了2个相同的查询。 有任何想法吗? 感谢

1 个答案:

答案 0 :(得分:2)

clientReports可能会在您每次枚举时执行查询(例如,获取Count())。

为避免这种情况,请在作业中使用.ToList()