如何对 dataset.table[0] 进行排序然后获取前 10 名?
我正在向表中添加一个自动增量列(称为“rowNum”),并且运行良好,之后我使用此代码对数据表行进行排序:
DataView dv = MyDataSet.Tables[0].DefaultView;
dv.Sort = "columnName DESC";
其中 columnName 是我的列之一(不是自动增量列)。
现在,问题是: 当我想获取前 10 行时,我使用以下代码:
dv.RowFilter = "rowNum <= 10";
结果不是我想要的,因为当我执行 dv.Sort 时,rowNum 被打乱(顺序错误)。
对行进行排序后如何获得前 10 行?
I'm adding an auto increment column (called "rowNum") to my table and it's working good, after that I use this code to sort datatable rows :
DataView dv = MyDataSet.Tables[0].DefaultView;
dv.Sort = "columnName DESC";
where columnName is one of my columns (not the auto increment one).
Now,The problem is:
when I want to get the top 10 rows I use this code :
dv.RowFilter = "rowNum <= 10";
The result is not what I want, because when I do dv.Sort
the rowNum shuffled (becomes in wrong order).
How can I get top 10 rows after sorting rows?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
对于这样的事情我更喜欢 LINQ。相反,我使用 System.Linq 并写入:,
然后绑定到“行”。
I prefer LINQ for stuff like this. Instead, I use System.Linq and write:
and then just bind to "rows".
如果在 AsEnumerable() 行中添加 .Take(10) 不起作用。但如果添加下面提到的第二行就可以了。
if add .Take(10) in AsEnumerable() line is not working. but if add second line as mentioned below is working.