I've got a System.Generic.Collections.List(Of MyCustomClass) type object.
Given integer varaibles pagesize and pagenumber, how can I query only any single page of MyCustomClass objects?
We can implement the paging using the Linq Skip and Take method.
You can use LINQ to query any enumerable collections such as List<T>, Array, or Dictionary<TKey,TValue>. The collection may be user-defined or may be returned by a . NET API.
LINQ is a data querying API that provides querying capabilities to . NET languages with a syntax similar to a SQL. LINQ queries use C# collections to return data. LINQ in C# is used to work with data access from sources such as objects, data sets, SQL Server, and XML.
If you have your linq-query that contains all the rows you want to display, this code can be used:
var pageNum = 3;
var pageSize = 20;
query = query.Skip((pageNum - 1) * pageSize).Take(pageSize);
You can also make an extension method on the object to be able to write
query.Page(2,50)
to get the first 50 records of page 2. If that is want you want, the information is on the solid code blog.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With