LINQ TOhibernate计数

LINQ TO Nhibernate Count

我正在尝试使用LINQ进行Nhibernate来获取数据库中表的计数。但是,我正在运行的代码正在拉回表中的所有记录,而不是从表中运行select count()。

这是我的代码-

1
2
3
4
5
6
public int GetTotalCount(Func<T, bool> where) {

            IQueryable< T > queryable = this._sessionManager.GetCurrentSession().Linq< T >().Where(where).AsQueryable();
            return queryable.Count();

}

我也尝试过-

1
2
3
4
5
    public int GetTotalCount(Func<T, bool> where)
    {
        IQueryable< T > queryable = this._sessionManager.GetCurrentSession().Linq< T >();
        return queryable.Count(where);
    }

两者都拉回整个数据集,而不是进行计数。有什么想法吗?

此外,我正在使用NHProf对其进行概要分析,因此我可以查询它是否正在运行,即

选择*
来自表


您的where参数必须为Expression<Func<T, bool>>;否则,您将所有内容加载到内存中并使用LINQ-to-objects。

简而言之:

1
2
3
4
5
6
7
8
public int GetTotalCount(Expression<Func<T, bool>> where)
{
    return _sessionManager
        .GetCurrentSession()
        .Linq< T >()
        .Where(where);
        .Count();
}