从DataTable中选择第二组20行 [英] Selecting second set of 20 row from DataTable

查看:160
本文介绍了从DataTable中选择第二组20行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个用以下示例列从SQL表填充的数据表

I have a DataTable I am populating from SQL table with the following example columns

  • ID
  • 类型

我正在用某种类型的行填充DataTable.我想从生成的数据表中选择第10-20行:

I am populating the DataTable with rows which are of a certain type. I want to select the rows 10 - 20 from my resulting DataTable:

Connect conn = new Connect();
SqlDataAdapter da = new SqlDataAdapter(SQL, conn.Connection());

//Creates data
DataTable d = new DataTable();
da.Fill(d);

DataRow[] result = d.Select();

在上面的代码中,我省略了主要的SQL,并且目前我没有为DataRow数组选择的内容.我找不到引用行号的方法.

In the above code I have omitted the main SQL, and currently I have no select for my DataRow array. I cannot find a way to reference the row numbers.

例如,我正在寻找类似Select("rownum > X && rownum < Y")

So for instance I am looking for something like Select("rownum > X && rownum < Y")

我已经在这里搜索了,其他许多资源也无济于事.任何线索都将非常方便,或者只是简单不可能.

I have searched here, and a number of other resources to no avail. Any clues would be really handy, or just a simple not possible.

推荐答案

总是最好从数据库中选择只需要的内容(使用TOP子句或ROW_NUMBER之类的窗口函数)而不是过滤它存储在内存中.

It's always better to select only what you need from the database(f.e. by using the TOP clause or a window function like ROW_NUMBER) instead of filtering it in memory.

但是,您可以使用Linq-To-DataSetEnumerable.Skip + Enumerable.Take:

However, you can use Linq-To-DataSet and Enumerable.Skip + Enumerable.Take:

var rows = d.AsEnumerable()
    .Skip(9).Take(11);  // select rows 10-20 as desired (so you want 11 rows)

如果要从过滤结果中得到新的DataTable,请使用CopyToDataTable,如果要DataRow[],请使用rows.ToArray().

If you want a new DataTable from the filtered result use CopyToDataTable, if you want a DataRow[] use rows.ToArray().

这篇关于从DataTable中选择第二组20行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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