The test below (which passes and was inspired by page 374 of C# 4.0 in a Nutshell) shows first how a query brings back four contacts and then how a second query brings back the two contacts in the middle of the four by skipping the first record and taking the next two. A tiny record set like this is hardly worthy of pagination, but I'm sure that you can see how IQueryable and LINQ may work well with paginated lists by utilizing Skip and Take.
using System.Collections.Generic;
using System.Data.Linq;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Whatever.Models;
using System.Linq;
namespace Whatever.Tests
{
[TestClass]
public class IQueryableTest
{
[TestMethod]
public void DataTest()
{
string Ok = @"server=.\sqlexpress;database=OK;Integrated Security=true;";
DataContext dataContext = new DataContext(Ok);
Table<Contact> contacts = dataContext.GetTable<Contact>();
IQueryable<Contact> query = contacts.OrderBy(c => c.City);
List<Contact> list = query.ToList();
Assert.AreEqual(list.Count(), 4);
Assert.AreEqual(list[0].Name, "Johnny");
Assert.AreEqual(list[1].Name, "Sid");
Assert.AreEqual(list[2].Name, "Paul");
Assert.AreEqual(list[3].Name, "Steve");
query = contacts.OrderBy(c => c.City).Skip(1).Take(2);
list = query.ToList();
Assert.AreEqual(list.Count(), 2);
Assert.AreEqual(list[0].Name, "Sid");
Assert.AreEqual(list[1].Name, "Paul");
}
}
}
No comments:
Post a Comment