代码改变世界

C#示例学习(八)-Linq to object(5)分页效果

2011-04-28 12:32  杨延成  阅读(689)  评论(0编辑  收藏  举报

1         public void TestMethod()
2         {
3             List<Book> bookList = new List<Book>
4             {
5                     new Book{Title="C#高级编程",Book_Author=new Author{Name="黄老邪",Address="桃花岛"},Price=128f},
6                     new Book{Title="Think in Java",Book_Author=new Author{Name="欧阳峰",Address="白驼山"},Price=128f},
7                     new Book{Title="silverlight",Book_Author=new Author{Name="一灯大师",Address="大理"},Price=128f},
8                     new Book{Title="JavaFx",Book_Author=new Author{Name="洪七公",Address="临安"},Price=125f},
9                     new Book{Title="ASP.Net服务器端开发",Book_Author=new Author{Name="裘千仞",Address="铁掌峰"},Price=125f},
10                   new Book{Title="Linq in Action",Book_Author=new Author{Name="王重阳",Address="九宫山"},Price=125f},
11                   new Book{Title="EJB3.0",Book_Author=new Author{Name="周伯通",Address="浪迹天涯"},Price=125f}
12                
13             };
14
15             var mybook = bookList
16                 .Select(book => new { Name = book.Title, MyAuthor = book.Book_Author.Name })
17                ///跳过2条
18                 .Skip(2)
19                 ///取出3条记录,有点象 top 3  可以与Skip一起做分页显示
20                 .Take(3);
21
22             foreach (var item in mybook)
23             {
24                 Console.WriteLine("书名:{0} 作者:{1}", item.Name, item.MyAuthor);
25             }
26
27
28
29         }