Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > Linq to SQL custom paging

Reply
Thread Tools

Linq to SQL custom paging

 
 
Mike
Guest
Posts: n/a
 
      11-15-2007
Not sure if this is the right group. Please point me to alternatives if it
isn't.

I'm playing with the Linq To Sql designer in VWD Orcas, and am looking at
how to apply custom paging to a GridView for display on a web page. I want
to use <a href> for paging because I don't like the javascript links that
the GridView employs with it's built-in paging. The following works:

protected void Page_Load(object sender, EventArgs e)
{
int startRow = Convert.ToInt32(Request.QueryString["StartRow"]);
PageProducts(startRow);
}

void PageProducts(int startRow)
{
NorthwindDataContext db = new NorthwindDataContext();

var query = from p in db.Products
where p.Order_Details.Count > 2
select new
{
ID = p.ProductID,
Name = p.ProductName,
NumOrders = p.Order_Details.Count,
Revenue = p.Order_Details.Sum(o => o.UnitPrice * o.Quantity)
};

int totalRecords = query.Count();
int pageSize = 10;
int totalPages = totalRecords/pageSize;
if (totalRecords % 10 > 0)
{
totalPages += 1;
}
StringBuilder sb = new StringBuilder();
for(int i = 0; i < totalPages; i++)
{
int pageNo = i + 1;
sb.Append("<a href=\"Paging.aspx?StartRow=" + pageSize * i + "\">" +
pageNo.ToString() + "</a>&nbsp;");
}

Literal1.Text = "Page: " + sb.ToString();

GridView1.DataSource = query.Skip(startRow).Take(10);
GridView1.DataBind();

}

But I am guessing that it causes two calls to the database: one to populate
totalRecords, which gets all the records for calculating the paging html,
and the second that retrieves just the records for display. Would I be
right in my guess, and if so, is there a more efficient way of obtaining the
count?



 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Is selecting column, filtering , sorting, paging is done in database or memory when use linq to sql? Ryan Liu ASP .Net Web Controls 0 12-12-2008 06:40 PM
Linq or not Linq George ASP .Net 4 11-05-2008 04:53 PM
Custom providers and linq to sql Andy B ASP .Net 0 10-31-2008 05:49 AM
LINQ to SQL and new MS SQL 2008 datatypes Mike Gleason jr Couturier ASP .Net 1 10-29-2008 03:42 AM
Finding and selecting a record in a gridview with paging and sortingenabled (using LINQ) Mike N. ASP .Net 0 07-31-2008 04:47 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57