public void QueryableExtensions_Test_ToPage()
        {
            IList<User> list = new List<User>();
            list.Add(new User(){Id = 1,Name = "zhangsan1",Age = 18,Address = "四川"});
            list.Add(new User() { Id = 2, Name = "zhangsan2", Age = 18, Address = "四川" });
            list.Add(new User() { Id = 3, Name = "zhangsan3", Age = 18, Address = "四川" });
            list.Add(new User() { Id = 4, Name = "zhangsan4", Age = 18, Address = "四川" });
            list.Add(new User() { Id = 5, Name = "zhangsan5", Age = 18, Address = "四川" });

            IQueryable<User> query = list.AsQueryable();

            PagedResult<User> page = query.ToPage<User>();
            PagedResult<User> page1 = query.ToPage<User>(new Pager(1,1));
            SortCondition[] sort = new SortCondition[] { new SortCondition<User>(o => o.Id, ListSortDirection.Descending) };
            PagedResult<User> page2 = query.ToPage<User>(o=>o.Name.Contains("zhangsan"), new Pager(1, 1, sort));

            Assert.AreEqual(list.Count, page.Data.Count);
            Assert.AreEqual(1, page1.Data.Count);
            Assert.AreEqual(1, page2.Data.Count);
            Assert.AreEqual(5, page2.Data.First().Id);

            var entity = query.FirstOrDefault();
            var entity1 = query.FirstOrDefault(o => o.Age == 20);
            var entity2 = query.Single();
        }
Esempio n. 2
0
 /// <summary>
 /// 初始化分页
 /// </summary>
 /// <param name="page">页索引</param>
 /// <param name="pageSize">每页显示行数,默认20</param> 
 /// <param name="sortConditions">排序条件</param>
 public Pager(int pageNumber, int pageSize, SortCondition[] sortConditions)
 {
     this.PageNumber = pageNumber;
     this.PageSize = pageSize;
     this.SortConditions = sortConditions;
 }