Reputation: 761
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.
Here's my code-
public int GetTotalCount(Func<T, bool> where) {
IQueryable<T> queryable = this._sessionManager.GetCurrentSession().Linq<T>().Where(where).AsQueryable();
return queryable.Count();
}
I also tried-
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?
Also, I'm using NHProf to profile it, so I can the query it is running, which is
select * from table
Upvotes: 2
Views: 3757
Reputation: 52725
Your where
parameter needs to be an Expression<Func<T, bool>>
; otherwise you are loading everything in memory and using LINQ-to-objects.
In short:
public int GetTotalCount(Expression<Func<T, bool>> where)
{
return _sessionManager
.GetCurrentSession()
.Linq<T>()
.Where(where);
.Count();
}
Upvotes: 7