Nhibernate Icreteria Paged Query

Source: Internet
Author: User
Tags rowcount

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.