LINQ TO休眠计数 [英] LINQ TO Nhibernate Count

查看:66
本文介绍了LINQ TO休眠计数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am trying to use LINQ to Nhibernate to get a count on a table in my database. However, the code I am running is pulling back all of the records in the table versus running select count() from table.

这是我的代码-

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

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

}

我也尝试过-

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

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

Both pull back the entire dataset versus running a count. Any ideas?

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

Also, I'm using NHProf to profile it, so I can the query it is running, which is

选择* 从表中

推荐答案

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

Your where parameter needs to be an Expression<Func<T, bool>>; otherwise you are loading everything in memory and using LINQ-to-objects.

简而言之:

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

这篇关于LINQ TO休眠计数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆