How to Use OrderBy in linq for DataTable in asp.net
asp.net, c#, linq
Solution
If your field data type is `DateTime` then you can use:
var sortedTable = Folder_table.AsEnumerable()
.OrderBy(r => r.Field<DateTime>("FolderDateTime"))
.CopyToDataTable();
If your column type is `string` then first you have to parse these values to `DateTime` type object. Use `DateTime.ParseExact` with format `d\M\yyyy` like:
var sortedTable = Folder_table.AsEnumerable()
.OrderBy(r => DateTime.ParseExact(r.Field<string>("FolderDateTime"),
"d/M/yyyy", CultureInfo.InvariantCulture))
.CopyToDataTable();
You should see: LINQ to DataSet/ DataTable - MSDN
Problem
I have two Data table Folder_table and folder_filter. Both the table has two columns `FolderName` and `FolderDateTime` . In the Folder_Table i have values like below ``` FolderName FolderDateTime Test1 29/3/2014 Test2 20/12/2014 Test3 4/2/2014 test5 9/6/2014 ``` I am using the below coding to copy 2 rows from Folder_table to folder_filter ``` folder_filter = Folder_table.Rows.Cast<System.Data.DataRow>().Take(2).CopyToDataTable(); ``` I want to order by descending in `FolderDateTime` column. how to do that. PLease help