如何对 dataset.table[0] 进行排序然后获取前 10 名?

发布于 2024-08-04 12:49:51 字数 378 浏览 11 评论 0原文

我正在向表中添加一个自动增量列(称为“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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

咋地 2024-08-11 12:49:51

对于这样的事情我更喜欢 LINQ。相反,我使用 System.Linq 并写入:,

var rows = MyDataSet.Tables[0].AsEnumerable()
   .OrderByDescending(r => r["columnName"]) 
   .Take(10);

然后绑定到“行”。

I prefer LINQ for stuff like this. Instead, I use System.Linq and write:

var rows = MyDataSet.Tables[0].AsEnumerable()
   .OrderByDescending(r => r["columnName"]) 
   .Take(10);

and then just bind to "rows".

凝望流年 2024-08-11 12:49:51

如果在 AsEnumerable() 行中添加 .Take(10) 不起作用。但如果添加下面提到的第二行就可以了。

var Rows = (from row in dt.AsEnumerable() 
orderby row["CostPerVeteran"] descending
select row);

dtChart = Rows.Take(10).CopyToDataTable();

if add .Take(10) in AsEnumerable() line is not working. but if add second line as mentioned below is working.

var Rows = (from row in dt.AsEnumerable() 
orderby row["CostPerVeteran"] descending
select row);

dtChart = Rows.Take(10).CopyToDataTable();

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文