Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

LINQ and paging with a DataTable - can't get Skip working?

Tags:

c#

linq

datatable

Ok so this might be a dumb question, but I can't seem to figure it out. I thought I'd try out LINQ against a DataTable. I got my query working and now I'm trying to implement some simple paging.

DataTable dataTable = null;

dataTable = GetAllDataTables();

var query = from r in dataTable.AsEnumerable()
            orderby r.Field<string>(Constants.fileName)
            select r;

query.Skip(WPP_PAGE_SIZE * pageIndex).Take(WPP_PAGE_SIZE);

My problem is that I get an error at query.Skip(...).

Error 1 'System.Data.OrderedEnumerableRowCollection' does not contain a definition for 'Skip' and no extension method 'Skip' accepting a first argument of type 'System.Data.OrderedEnumerableRowCollection' could be found (are you missing a using directive or an assembly reference?)

References I have:

  • Microsoft.SharePoint
  • System
  • System.Core
  • System.Data
  • System.Data.DataSetExtensions
  • System.Web
  • System.Xml

What am I missing?

like image 951
Kit Menke Avatar asked Jun 16 '10 17:06

Kit Menke


People also ask

Which LINQ methods should you use to implement pagination?

We can implement the paging using the Linq Skip and Take method.

Does LINQ improve performance?

Often, developing a solution using LINQ will offer pretty reasonable performance because the system can build an expression tree to represent the query without actually running the query while it builds this. Only when you iterate over the results does it use this expression tree to generate and run a query.


1 Answers

You need using System.Linq; at the top of your file.

A second problem is that you need to assign the result of Skip and Take to something otherwise the result is simply discarded:

var query2 = query.Skip(WPP_PAGE_SIZE * pageIndex).Take(WPP_PAGE_SIZE);
like image 130
Mark Byers Avatar answered Sep 29 '22 21:09

Mark Byers