Jankya
Jankya

Reputation: 958

Datatable show data according condition Sequence

I have a data table with some records and have filter condition i.e

datatable.select("ColumnName like '090%' or ColumnName like '070%' or ColumnName like '080%'"). 

Now i wanted the datatable result like to arrange data like condition sequence means first records should be related to '090%' then '070%' and '080%'.

Can anyone help me for that?

Upvotes: 1

Views: 130

Answers (1)

Anthony Chu
Anthony Chu

Reputation: 37530

Select() returns an array of DataRows. You should be able to use LINQ to sort them...

// using System.Linq;

var selectedRows = datatable.Select("ColumnName like '090%' or ColumnName like '080%' or ColumnName like '070%'");
var sortedSelectedRows = selectedRows.OrderByDescending(r => r["ColumnName"].ToString()).ToArray();

--- update ---

If you want to do this in a specific order that can't be sorted with OrderBy, I'm not sure if there's an easy way. Maybe try something like this...

// using System.Linq;

var group1 = datatable.Select("ColumnName like '090%');
var group2 = datatable.Select("ColumnName like '070%');
var group3 = datatable.Select("ColumnName like '080%');

var selectedRows = group1.Concat(group2).Concat(group3).ToArray();

I wouldn't do this if there are lots of rows in the original DataTable though. Performance would be pretty bad.

Upvotes: 1

Related Questions