Getting Nth value with Linq
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();
精彩评论