Linq Take和Skip详解

it2026-02-07  1

Take()方法的作用就是:从查询结果中提取前n个结果。

Skip()方法正好是Take()方法的反面,它可以跳过前n个结果,返回剩余的结果。

例如:查找年龄最大的3个人

表Student的数据是

NameAgeJame21Tom56KD36Black5Bules12

 

 

 

 

 

 

使用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 }

 

转载于:https://www.cnblogs.com/dehuachenyunfei/p/6836784.html

最新回复(0)