Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
Question
Tuesday, July 19, 2011 6:46 AM
Hi! I want to select only 10 rows from a dataTable each time, just as
from a database("SELECT TOP 10 FROM orders WHERE ID <100 ORDER BY
ID"). for example:
DataTable myTable = new DataTable("orders");
DataRow[] rows = myTable.Select("TOP 10 ID < 100");
1)How can I write the correct statement.
2)Can I ues "TOP" key word or there's other way to do it?
I mean from a dataTable.
thanks
Thanks Motevallizadeh
All replies (6)
Tuesday, July 19, 2011 6:54 AM âś…Answered | 1 vote
Hello
You can select top 10 value from datatable like
DataRow[] rows = table.Select("ID < 100 ");
Thanks
sankar
Please mark the post answered your question as the answer, and mark other helpful posts as helpful, so they will appear differently to other users who are visiting your thread for the same problem.
Tuesday, July 19, 2011 6:50 AM
You can use Linq (total of 100):
var topRows = table.AsEnumerable().Take(10).ToList();
if you want to select last 10 you do:
IEnumerable<DataRow> lastRows = table.AsEnumerable().Skip(table.Rows.Count - 10).ToList();
Return type is DataRow, asI have showed in both cases.
Mitja
Tuesday, July 19, 2011 6:55 AM
You can use Linq:
var topRows = table.AsEnumerable().Take(10).ToList();
if you want to select last 10 you do:
IEnumerable<DataRow> lastRows = table.AsEnumerable().Skip(2).ToList();
Return type is DataRow, asI have showed in both cases.
Mitja
its good but you do not use >> WHERE ID <100 in your syntax
Thanks Motevallizadeh
Tuesday, July 19, 2011 6:59 AM
Or if you wanna get 1st top 10 orderedby ID columns:
var topRows = table.AsEnumerable().OrderBy(o=> o.Field<int>("IDColumn")).Take(2).ToList(); //IdColumn is your actual ID field
Mitja
Tuesday, July 19, 2011 7:01 AM
And with WHERE clause:
var topRows = table.AsEnumerable().OrderBy(o => o.Field<int>("IDColumn")).Where(w => w.Field<int>("IDColumn") < 100).ToList();
Mitja
Wednesday, August 3, 2011 11:37 AM
Can you please clarify how we get only 10 rows from this method. It is simple filtering, without getting top 10 rows. ThanksSumit Gupta http://www.sumitgupta.net