如何获得前10名行从排序的数据表? [英] How to get Top 10 rows from a sorted DataTable?

查看:112
本文介绍了如何获得前10名行从排序的数据表?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想获得前10名来自行以相同的顺序为previous数据表的数据表。

随着code我下面,我可以把它排序DT1和进口只有10行到DT2,但它不是进口的行的排序顺序。我需要它保持有序。您的帮助将非常AP preciated。

  DataTable的DT2 = dt1.Clone();
    dt1.DefaultView.Sort =的X DESC;
    的for(int i = 0;我小于10;我++)
    {
        DT2 .ImportRow(dt1.Rows [I]);
    }
 

解决方案

只需使用一个查询,这样的排序:

  DataTable的DT2 = dt.Clone();

//只得到你想要的行
的DataRow []结果= dt.Select(,X DESC);

//填充新的目标表
为(变种I = 0; I&小于10;我+ +)
    dt2.ImportRow(结果[I]);
 

希望它能帮助!

I want to get top 10 rows from a DataTable in the same order as the previous DataTable.

With the code I have below, I can have it sorted in dt1 and import only 10 rows into dt2 but it's not importing the rows in the sorted order. I need it to keep the sorted order. Your help will be much appreciated.

    DataTable dt2 = dt1.Clone(); 
    dt1.DefaultView.Sort = "x DESC"; 
    for (int i = 0; i < 10; i++)
    {
        dt2 .ImportRow(dt1.Rows[i]);
    }

解决方案

Just use a query and sorting like this:

DataTable dt2 = dt.Clone();

//get only the rows you want
DataRow[] results = dt.Select("", "x DESC");

//populate new destination table
for(var i=0; i < 10; i++)        
    dt2.ImportRow(results[i]);

Hope it helps!

这篇关于如何获得前10名行从排序的数据表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆