tags:

views:

53

answers:

1

I'm not sure how to add paging to this:

Session.Linq<Article>()
  .Where(art => art.Tags.Any(t => t.Name == tag)).ToList().

So i decided to use Criteria API.

var rowCount = Session.CreateCriteria(typeof(Article))
  .SetProjection(Projections.RowCount()).FutureValue<Int32>();

var res = Session.CreateCriteria(typeof(Article))
  .Add(/* any help with this? :) */)
  .SetFirstResult(page * pageSize)
  .SetMaxResults(pageSize)    
  .AddOrder(new Order("DatePublish", true))
  .Future<Article>();

totalCount = rowCount.Value;

Any help appreciated.

+2  A: 

In link to do paging you use the commands Skip and Take.

Your scenario:

Session.Linq<Article>()
  .Where(art => art.Tags.Any(t => t.Name == tag))
  .Skip(2*20).Take(20)
  .ToList();

int totalCount = Session.Linq<Article>()
                     .Where(art => art.Tags.Any(t => t.Name == tag))
                     .Count();
smaclell
Yeah, but how to retrieve total count? And how to do that in same database request? I don't want to retrieve thousands of articles in memory.
Arnis L.
I have not done a tonne of this but I pretty sure in most cases you just call the count method. Depending on what LINQ provider you are using there might be a better way to get the count after a query has executed. The NHibernate LINQ Provider may have a better way to do this I just thought I would provide the bare minimum LINQ.
smaclell
I'm quite sure this will result in 2 database hits. Will be surprised if it won't. Will try and post profiler log. :)
Arnis L.
Ya I fairly certain this will hit the database twice.
smaclell
Yap... 2 selects (i did try to leave first expression result as IQueryable until 2nd select returns count). At the moment - i don't care much (facing deadlines), but it's not the right answer to question (actually - knew this myself). :)
Arnis L.