C#: Sorting a DataTable
Posted on by
Sorting the result in a DataTable’s select command can be done as below.
1 2 | DataTable table = dataSet.Tables[0]; // Get a datatable from the dataset. DataRow[] row = table.Select("id = 500", "name desc, age asc"); // Get from table, where id = 500 order by name desc and age desc. |
The first argument to the select is the filter condition, the second argument is the sort option.
Trying to use,
1 2 | DataTable table = dataSet.Tables[0]; // Get a datatable from the dataset. DataRow[] row = table.Select("id = 500 order by name desc, age asc"); // Get from table, where id = 500 order by name desc and age desc. |
will result in a error “Missing operand before order”.
Possibly Related posts:
- C#: Null coalescing operator C# has a ?? operator, which is called the ‘Null...
- Converting Rows to Columns in SQL There was this table, ORG_ID LEGACY_ORG_CODE ------ ------------------ 1001 8909...
- C#: Calculate Age in Years, Month and Days Today I was given the task of finding the age...
- Implicit Variable in C# 2008 C# 2008 allows for creating implicit variables using the ‘var’...
- Rake Migrate – Aborted! Trying out a sample application using rails, I faced a...
Tags: C#, Programming