8

I have 100 records in my Datable says to be in

DataTable dt=new DataTable();

dt have 100 of records say column name as sub_id(contain int datatype) and subheadername(contain nvarchar(150)) , I want top 20 records from this dt in ascending order

I am putting code as

//dtlcategories.DataSource = dt.AsEnumerable().OrderBy(x => x["subheadername"]).Take(20).ToList();
dtlcategories.DataSource = dt.Rows.Cast<DataRow>().OrderBy(x => x["subheadername"]).Take(20).ToList();
dtlcategories.DataBind();

Here dtlcategories is Datalist but on running error is coming as 'System.Data.DataRow' does not contain a property with the name 'subheadername'.

ANSWER IS SOLVED

dtlcategories.DataSource = dt.Rows.Cast<DataRow>().OrderBy(x => x["subheadername"]).Take(20).copytodatatable();
dtlcategories.DataBind();

5 Answers5

11

There's a couple different ways you can do this using LINQ. These will both return the same results.

dt.AsEnumerable().OrderBy(x => x["subheadername"]).Take(20);

dt.Rows.Cast<DataRow>().OrderBy(x => x["subheadername"]).Take(20);

If you're going to use the result as the source of data for another control, you may need to call .ToList() after .Take(x).

Edit:

I changed the column name based on your edit. If you want to sort by id instead (you didn't specify), just replace "subheadername" with "sub_id".

Grant Winney
  • 65,241
  • 13
  • 115
  • 165
  • Please see my edited question and edit your answer as per my column name and in orderby ascending –  Dec 21 '13 at 12:28
  • how should i take this value in my datalist.datasource?? –  Dec 21 '13 at 12:39
  • i am putting like ... dt.AsEnumerable().OrderBy(x => x["subheadername"]).Take(20).ToList(); dtlcategories.DataSource = dt.Rows.Cast().OrderBy(x => x["subheadername"]).Take(20).ToList();....butt error is coming as 'System.Data.DataRow' does not contain a property with the name 'subheadername'. –  Dec 21 '13 at 12:57
  • i have edited my question, please made me know whats my mistake?? –  Dec 21 '13 at 13:02
  • If i used to put sub_id than error as "System.Data.DataRow' does not contain a property with the name 'sub_id " –  Dec 21 '13 at 13:12
2

This query fetches top 20 records from db and then orders them by the sub_id column.

var topTwenty = dt.AsEnumerable().Take(20).OrderBy(r => r.Field<int>("sub_id"));
Ondrej Janacek
  • 12,486
  • 14
  • 59
  • 93
  • @GrantWinney That's the way I understand the question, though. – Ondrej Janacek Dec 21 '13 at 12:08
  • Please see my edited question and edit your answer as per my column name and in orderby ascending –  Dec 21 '13 at 12:29
  • if i want to order this on subheadername than i used to take nvarchar(150) in place of int??? –  Dec 21 '13 at 12:34
  • @Xtremcool no, C# does not recognize `nvarchar(150)` as a type, you have to use `string` instead – Ondrej Janacek Dec 21 '13 at 12:37
  • i am getting error "System.InvalidCastException: Specified cast is not valid."...when i used to fill this record in my datalist tool like dtlcategories.DataSource = dt.AsEnumerable().Take(20).OrderBy(r => r.Field("sub_id")); dtlcategories.DataBind(); –  Dec 21 '13 at 12:38
  • try appending `.Cast()` as in the Grant Winney's answer – Ondrej Janacek Dec 21 '13 at 12:39
  • i have edited my question, please made me know whats my mistake?? –  Dec 21 '13 at 13:02
  • you have "sibheadername" instead of "subheadername" – Ondrej Janacek Dec 21 '13 at 13:04
1
dt.AsEnumerable().OrderBy(row => row["sub_id"]).Take(20);

This will return you IEnumerable. Now iterate through the IEnumerable and add them to another data table. Now your final data table is ready!!

Arindam
  • 320
  • 2
  • 12
  • Please see my edited question and edit ypur answer as per my column name –  Dec 21 '13 at 12:27
1

this code orders data according to date and takes first 100 row.

 var table = new DataTable();
 var t = table.AsEnumerable();
 var result = t.OrderByDescending(f => f.Field<DateTime>(new DataColumn("Date"))).Take(100);

Update:

var table = new DataTable();
var t = table.AsEnumerable();
var result = t.OrderBy(f => f.Field<String>(new DataColumn("subheadername"))).Take(20)
SHM
  • 1,896
  • 19
  • 48
  • Please see my edited question and edit your answer as per my column name and in orderby ascending –  Dec 21 '13 at 12:29
0

A possible solution:

DataRow[] rows = dt.Select("sub_id< 100 ");
Rubens
  • 14,478
  • 11
  • 63
  • 92
Vignesh Kumar A
  • 27,863
  • 13
  • 63
  • 115
  • Please see my edited question and edit ypur answer as per my column name –  Dec 21 '13 at 12:26