I just finished reading Mike’s awesome tutorial over at: http://www.mikesdotnetting.com/Article/150/Web-Pages-Efficient-Paging-Without-The-WebGrid
and I am using SQL CE 4.0 with Entity F/W ADO.NET and my search query is:
foreach(string term in query)
{
var products = database.Products.Where(p =>
p.PartNumber.ToLower().Contains(term.ToLower()) ||
p.PartNumber.ToLower() == term.ToLower() || p.OProductName.ToLower().Contains(term.ToLower()) || p.OProductName.ToLower() == term.ToLower());
}
In Mike’s search query:
sql = "Select Title, ISBN, Description, FirstName, LastName, Category From Books " +
"Inner Join Authors on Books.AuthorId = Authors.AuthorId " +
"Inner Join Categories on Books.CategoryId = Categories.CategoryId " +
"Order By BookId OFFSET @0 ROWS FETCH NEXT @1 ROWS ONLY;";
var result = db.Query(sql, offset, pageSize);
He uses the normal “WebMatrix way” of performing select query with database.
The part that has me stuck, is how to I write his OFFSET @0 ROWS FETCH NEXT @1 ROWS ONLY; in my version of the query? How do I write it in EF ado.net?
I hope this makes sense.
What you are looking for are the LINQ methods Skip() and Take(). Skip() lets you skip records – the OFFSET @0 ROWS portion of the query. Take() lets you specify how many rows to return – the FETCH NEXT @1 ROWS ONLY portion of the query.
So your code to return PageSize items from page Page, might look something like this:
Edit: You’ll also need to throw in an OrderBy() before the Skip(), as only ordered queries can be skipped. So you might want to order by part number, for example.
As an aside, it’s a good thing you are using SQL CE 4.0, as previous versions did not support Skip() in EF!!