Hi,
I have a DataTable
that contains 2000 records.
How would you retrieve the first 100 records in the DataTable
?
Hi,
I have a DataTable
that contains 2000 records.
How would you retrieve the first 100 records in the DataTable
?
If it implements IEnumerable<T>
:
var first100 = table.Take(100);
If the type in question only implements IEnumerable, you can use the Cast extention method:
var first100 = table.Cast<Foo>().Take(100);
And to make the list full, here is the statement for MS SQL:
Select top 5 * from MyTable2
And some other methods with MS SQL can be found here.
To get a list of the top n records in C# using the 2.0 framework:
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]);
}