开发者

Return next 5 rows using LINQ in C# ASP.NET?

开发者 https://www.devze.com 2023-01-12 22:02 出处:网络
I\'m using LINQ to query my database, and I don\'t want all the records returned all at once. Instead, user should be able to select Next 5 and Previous 5 using buttons. As of this moment I have selec

I'm using LINQ to query my database, and I don't want all the records returned all at once. Instead, user should be able to select Next 5 and Previous 5 using buttons. As of this moment I have selected the first 5 rows in the Item table in the databas like this:

private void ShowItems()
{
    var items = (from p in db.Items
                 orderby p.ID descending
                 select new { ID = p.ID, Item = p.itemName, 
                     Qty = p.quantity }).Take(5);

    GridView.DataSource 开发者_JS百科= items;
    GridView.DataBind();
}

So my idea was to change the Take() to something like Take(from, to), but it isn't supported. What do I do to solve my problem?

Thanx for listening!


This should do it:

.Skip((pageNo - 1) * resultsPerPage)
.Take(resultsPerPage)


You can use the Skip and Take methods to achieve what you want.


public static IEnumerable<T> Take<T>(this IEnumerable<T> collection, int start, int end)
{
    if (start > end) 
        throw new ArgumentException("Invalid parameters");
    return collection.Skip(start).Take(end - start);
}
0

精彩评论

暂无评论...
验证码 换一张
取 消