开发者

Getting Nth value with Linq

开发者 https://www.devze.com 2022-12-22 21:11 出处:网络
How 开发者_运维知识库can I get the Nth row using Linq? both columns are text so I cant use min/maxvar nthItem = items.Skip(n).First();

How 开发者_运维知识库can I get the Nth row using Linq? both columns are text so I cant use min/max


var nthItem = items.Skip(n).First();


An alternative (.Net 3.5 and later) is to use ElementAtOrDefault.

var nthItem = items.ElementAtOrDefault(n-1);

The method's index is zero-based, so if you want the third element, you pass 2 for the index.


You can use skip and take.

var result = myData.OrderBy(<your order by>).Skip(5).Take(1);


var nthItem = items.Skip(n-1).FirstOrDefault();


you can use order by with skip

var nthItem = items.OrderByDescending(<your order by>).skip(n-1).FirstOrDefault();
0

精彩评论

暂无评论...
验证码 换一张
取 消