LINQ TO Nhibernate Count

Bri*_*ian 2 linq nhibernate

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

这是我的代码 -

public int GetTotalCount(Func<T, bool> where) {

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

}
Run Code Online (Sandbox Code Playgroud)

我也试过 -

    public int GetTotalCount(Func<T, bool> where)
    {
        IQueryable<T> queryable = this._sessionManager.GetCurrentSession().Linq<T>();
        return queryable.Count(where);
    }
Run Code Online (Sandbox Code Playgroud)

两者都拉回整个数据集而不是运行计数.有任何想法吗?

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

从表中选择*

Die*_*hon 6

你的where参数需要是一个Expression<Func<T, bool>>; 否则你将所有内容加载到内存中并使用LINQ-to-objects.

简而言之:

public int GetTotalCount(Expression<Func<T, bool>> where)
{
    return _sessionManager
        .GetCurrentSession()
        .Linq<T>()
        .Where(where);
        .Count();
}
Run Code Online (Sandbox Code Playgroud)