1, create the query condition, the condition is a icreterion list
///
Create criteria (does not include order, because when you get the total, for performance reasons, do not add order)
///
Type
Icriterion List
///
Public Icriteria Createcriteria (Type T, List List)
{
Icriteria criteria = Session.createcriteria (t);
if (list! = null && list. Count > 0)
{
foreach (icriterion C in list)
{
Criteria. ADD (c);
}
}
return criteria;
}
2, paging query
///
Get the sorted PageInfo
///
Type
Icriterion List
Ascending? true:false
Sort fields
///
Public PageInfo Getorderdpagedlistbycriteria (PageInfo pageinfo,type T, List List, bool Isasc, string by)
{
Icriteria c= Createcriteria (t, list);//Stitching query conditions
int startnum = Pageinfo.currentpage * pageinfo.pagesize;
Create an identical Icriteria object, or after the total query is executed, the Icriteria condition remains the total query, and the result query below will be faulted.
Icriteria Pagecriteria = Criteriatransformer.clone (c);
Executing a result query, the projections object is projected into a new query that contains AVG, SUM, Count, and so on
pageinfo.rowcount = Int. Parse (Pagecriteria.setprojection (Projections.rowcount ()). Uniqueresult (). ToString ());
After the totals query is executed, the collation is created for the previous Icriteria object
if (ISASC)
C.addorder (New Order (), true);
Else
C.addorder (new Order, false));
After you create the collation, perform a data query
Pageinfo.results = C.setfirstresult (startnum). Setmaxresults (pageinfo.pagesize). List (). ToList ();
return pageInfo;
}
Note: PageInfo is a paging class
///
Results
///
Public List Results {get; set;}
///
Current page number
///
public int CurrentPage {get; set;}
///
Total pages (How many pages there are altogether)
///
public int PageCount
{
Get
{
if (RowCount = = 0)
{
return 1;
}
Else
{
return (int) math.ceiling (RowCount/(double) PageSize);
}
}
Private Set
{
This. PageCount = value;
}
}
///
Total Record Count
///
public int RowCount {get; set;}
///
Number of records per page
///
private int pagesize;
public int PageSize
{
get {return pagesize;}
set {pagesize = value;}
}
}
Nhibernate Icreteria Paged Query