[英]How do I get the last 5 rows of a datatable?
How do I get the last 5 rows of a datatable?如何获取数据表的最后 5 行? I tried something like this:
我试过这样的事情:
var Long_bottom = LongSlection.Last(5);
Where LongSlection is a DataRow.其中 LongSlection 是一个 DataRow。 But I had an error, any idea?
但我有一个错误,知道吗?
Not sure what you have got here不确定你在这里得到了什么
var Long_bottom = LongSlection.Last(5);
var Long_bottom = LongSlection.Last(5);
Assuming you have a DataTable and you want to get the last 5 rows you can do that via假设您有一个 DataTable 并且您想获得最后 5 行,您可以通过
datatable1.AsEnumerable().Reverse().Take(5);
Take
and Skip
return the specific number of elments (parameter is int) while which is not the case with Last
you get the last element or you need a predicate for checking conditions or checks within the row. Take
和Skip
返回特定数量的元素(参数是 int),而Last
不是这种情况,你得到最后一个元素,或者你需要一个谓词来检查条件或检查行内的检查。
This SHOULD do it.这应该这样做。 but it's untested!
但它未经测试!
IEnumerable<DataRow> lastRows = table.AsEnumerable().Skip(table.Rows.Count - 2).ToList();
Or:要么:
table.AsEnumerable().Reverse().Take(2);
Edit: Modified to get the last TWO, as per the OPs request.编辑:根据 OP 请求修改为获取最后两个。
How about怎么样
Datarow[] dr = dt.Rows.Cast<DataRow>().Skip(dt.Rows.Count - 5).ToArray();
OR要么
dt.Rows.Cast<DataRow>().Reverse().Take(5);
Here 5 is the last number of rows to take.这里 5 是最后要取的行数。
我需要CopyToDataTable()
来完成任务:
dt = dt.AsEnumerable().Reverse().Take(5).CopyToDataTable();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.