C #: get first n records from DataTable

I have a DataTablecontaining 2000 entries.

How would you extract the first 100 entries in DataTable?

+3
source share
6 answers

If it implements IEnumerable<T>:

var first100 = table.Take(100);

If the type in question implements only IEnumerable, you can use the Cast extention method:

var first100 = table.Cast<Foo>().Take(100);
+11
source

This works for DB2.

select * from table
fetch first 100 rows only;
0
source

mysql: select * from table limit 100

0

- this, foreach 100 .

0

, MS SQL:

Select top 5 * from MyTable2

MS SQL .

0

n # 2.0:

DataTable dt = new DataTable();
var myRows = new List<DataRow>();

//no sorting specified; take straight from the top.
for (int i = 0; i < 100; i++)
{
   myRows.Add(dt.Rows[i]);
}
0

Source: https://habr.com/ru/post/1735431/


All Articles