Limit the data request and get only the last 1000 rows

I am using linq for nhibernate to query data from a large table. I am trying to limit my data request and get only the last 1000 rows and only then do filtering, sorting and pagin.

When I try to use .Take (), I got an error:

unable to locate HQL query plan in cache; generating (.Count[X4Data.Entity.IEventView](.OrderBy[X4Data.Entity.IEventView,System.DateTime](.Take[X4Data.Entity.IEventView](.Where[X4Data.Entity.IEventView](NHibernate.Linq.NhQueryable`1[X4Data.Entity.IEventView], Quote((x, ) => (Equal(x.DeviceId, p1))), ), p2, ), Quote((c5d4d87c-87ba-4e91-9652-bcdc87e3f0ba, ) => (c5d4d87c-87ba-4e91-9652-bcdc87e3f0ba.AtmTime)), ), )) 

My code is:

 query = query.Take(rowCount); query = query.ApplyFiltering(cmd, binder); query = query.ApplySorting(cmd, binder); binder.TotalCount = query.Count(); query = query.ApplyPaging(cmd); 

Thank you very much and sorry for my bad english

+4
source share
3 answers

Try:

 query.OrderByDescending(criteria).Take(rowCount).OrderBy(criteria) 
+5
source

You can use this:

 list.Skip(Math.Max(0, list.Count() - N)).Take(N); 

Example:

Here you create a list that contains 9999 values ​​and selects the last 1000 values ​​using LINQ .

  List<int> list = new List<int>(); for (int i = 0; i < 9999; i++) { list.Add(i); } int take = 1000; var result = list.Skip(Math.Max(0, list.Count() - take)).Take(take); 
+2
source

using ICriteria try the following:

  criteria.SetMaxResults(1000); criteria.AddOrder(Order.Desc("ID")); 
+1
source

Source: https://habr.com/ru/post/1413753/


All Articles