Jak można zrobić stronicowanie z NHibernate?

Na przykład chcę wypełnić kontrolkę gridview w ASP.NET strona z wyświetlanymi tylko danymi niezbędnymi dla # wierszy. Jak NHibernate może to wspierać?

Author: Ray, 2008-09-10

8 answers

ICriteria posiada metodę SetFirstResult(int i), która wskazuje indeks pierwszego elementu, który chcesz uzyskać (w zasadzie pierwszy wiersz danych na stronie).

Posiada również metodę SetMaxResults(int i), która wskazuje liczbę wierszy, które chcesz uzyskać (np. rozmiar strony).

Na przykład Ten obiekt criteria otrzymuje pierwsze 10 wyników twojej siatki danych:

criteria.SetFirstResult(0).SetMaxResults(10);
 112
Author: Jon Limjap,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2016-02-02 20:56:32

Możesz również skorzystać z funkcji Futures w NHibernate, aby wykonać zapytanie, aby uzyskać całkowitą liczbę rekordów, a także rzeczywiste wyniki w jednym zapytaniu.

Przykład

 // Get the total row count in the database.
var rowCount = this.Session.CreateCriteria(typeof(EventLogEntry))
    .Add(Expression.Between("Timestamp", startDate, endDate))
    .SetProjection(Projections.RowCount()).FutureValue<Int32>();

// Get the actual log entries, respecting the paging.
var results = this.Session.CreateCriteria(typeof(EventLogEntry))
    .Add(Expression.Between("Timestamp", startDate, endDate))
    .SetFirstResult(pageIndex * pageSize)
    .SetMaxResults(pageSize)
    .Future<EventLogEntry>();

Aby uzyskać całkowitą liczbę rekordów, wykonaj następujące czynności:

int iRowCount = rowCount.Value;

Dobra dyskusja o tym, co daje przyszłość, jest tutaj .

 87
Author: Jeremy D,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2009-08-25 15:34:18

Z NHibernate 3 i wyżej, możesz użyć QueryOver<T>:

var pageRecords = nhSession.QueryOver<TEntity>()
            .Skip((PageNumber - 1) * PageSize)
            .Take(PageSize)
            .List();

Możesz również wyraźnie zamówić wyniki w następujący sposób:

var pageRecords = nhSession.QueryOver<TEntity>()
            .OrderBy(t => t.AnOrderFieldLikeDate).Desc
            .Skip((PageNumber - 1) * PageSize)
            .Take(PageSize)
            .List();
 47
Author: Leandro de los Santos,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2018-12-29 13:16:17
public IList<Customer> GetPagedData(int page, int pageSize, out long count)
        {
            try
            {
                var all = new List<Customer>();

                ISession s = NHibernateHttpModule.CurrentSession;
                IList results = s.CreateMultiCriteria()
                                    .Add(s.CreateCriteria(typeof(Customer)).SetFirstResult(page * pageSize).SetMaxResults(pageSize))
                                    .Add(s.CreateCriteria(typeof(Customer)).SetProjection(Projections.RowCountInt64()))
                                    .List();

                foreach (var o in (IList)results[0])
                    all.Add((Customer)o);

                count = (long)((IList)results[1])[0];
                return all;
            }
            catch (Exception ex) { throw new Exception("GetPagedData Customer da hata", ex); }
      }

Gdy dane stronicowania jest inny sposób, aby uzyskać wpisywane wynik z MultiCriteria lub każdy robi to samo tak jak ja ?

Thanks

 31
Author: Barbaros Alp,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2009-01-11 17:01:29

A może użyjesz Linq do Nhibernacji, jak omówiono w ten post na blogu autorstwa Ayende?

Próbka Kodu:

(from c in nwnd.Customers select c.CustomerID)
        .Skip(10).Take(10).ToList(); 

A oto szczegółowy post na blogu zespołu NHibernate na temat dostępu do danych za pomocą NHibernate , w tym implementacji stronicowania.

 23
Author: NotMyself,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2008-09-10 17:33:19

Najprawdopodobniej w widoku GridView będziesz chciał pokazać kawałek danych plus całkowitą liczbę wierszy (rowcount) całkowitej ilości danych, które pasują do twojego zapytania.

Aby wysłać zapytanie Select count (*), należy użyć MultiQuery .SetFirstResult (n).SetMaxResult (m) queries do bazy danych w jednym wywołaniu.

Uwaga wynikiem będzie lista zawierająca 2 listy, jedną dla plasterka danych i jedną dla liczenia.

Przykład:

IMultiQuery multiQuery = s.CreateMultiQuery()
    .Add(s.CreateQuery("from Item i where i.Id > ?")
            .SetInt32(0, 50).SetFirstResult(10))
    .Add(s.CreateQuery("select count(*) from Item i where i.Id > ?")
            .SetInt32(0, 50));
IList results = multiQuery.List();
IList items = (IList)results[0];
long count = (long)((IList)results[1])[0];
 11
Author: zadam,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2008-09-26 06:23:26

Proponuję stworzyć specyficzną strukturę do radzenia sobie z paginacją. Coś w stylu (jestem programistą Javy, ale to powinno być łatwe do zmapowania):

public class Page {

   private List results;
   private int pageSize;
   private int page;

   public Page(Query query, int page, int pageSize) {

       this.page = page;
       this.pageSize = pageSize;
       results = query.setFirstResult(page * pageSize)
           .setMaxResults(pageSize+1)
           .list();

   }

   public List getNextPage()

   public List getPreviousPage()

   public int getPageCount()

   public int getCurrentPage()

   public void setPageSize()

}

Nie dostarczyłem implementacji, ale możesz użyć metod zaproponowanych przez @Jon . Oto Dobra dyskusja dla ciebie, aby spojrzeć.

 6
Author: Marcio Aguiar,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2017-05-23 12:02:37

Nie musisz definiować 2 kryteriów, możesz zdefiniować jedno i je sklonować. Aby sklonować kryteria nHibernate możesz użyć prostego kodu:

var criteria = ... (your criteria initializations)...;
var countCrit = (ICriteria)criteria.Clone();
 0
Author: Marcin,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2020-04-15 08:37:04