Linq Take和Skip详解
Take()方法的作用就是:从查询结果中提取前n个结果。
Skip()方法正好是Take()方法的反面,它可以跳过前n个结果,返回剩余的结果。
例如:查找年龄最大的3个人
表Student的数据是
| Name | Age | 
| Jame | 21 | 
| Tom | 56 | 
| KD | 36 | 
| Black | 5 | 
| Bules | 12 | 
使用SQL语句查询时,代码如下所示:
select top 3 from Student order by Age desc
使用Take()方法结合OrderByDescending子句一起来实现这个功能。
1 using (ITPDBEntities context = new ITPDBEntities()) 2 { 3 var query = from student in context.Student 4 select student; 5 //年龄最大的3个学生是:Tome,KD,Jame 6 var top3Student = query.OrderByDescending(it => it.Age).Take(3); 7 //其他的学生是:Bules,Black 8 var otherStudent = query.OrderByDescending(it => it.Age).Skip(3); 9 }
 
                    
                     
                    
                 
                    
                 
 
                
            
         
         浙公网安备 33010602011771号
浙公网安备 33010602011771号