rahul aggarwal
rahul aggarwal

Reputation: 265

using select in datatable

My Datatable is in following format.

enter image description here

I want to get the Netfare Where Sector is 1 and then similarly I want to get Netfare Where Sector is 2.

Thanks

Upvotes: 0

Views: 209

Answers (1)

Sachin
Sachin

Reputation: 40970

You can use the DataTable.Select method to filter the result.

var sector1Results = dt.Select("Sector = 1");
var sector2Results = dt.Select("Sector = 2");

You can also use DataTable.AsEnumerable method to achieve the same

var result1 = dt.AsEnumerable().Where(x => x.Field<int>("Sector") == 1).Select(x => x.Field<int>("Sector1"));

var result2 = dt.AsEnumerable().Where(x => x.Field<int>("Sector") == 2).Select(x => x.Field<int>("Sector2"));

To select the DataRow collection, You can use this

List<DataRow> collection1 = dt.AsEnumerable().Where(x => x.Field<int>("Sector") == 1).ToList();

List<DataRow> collection2 = dt.AsEnumerable().Where(x => x.Field<int>("Sector") == 1).ToList();

You can also merge those condition in single statement (if you want)

List<DataRow> collection = new DataTable().AsEnumerable().Where(x => x.Field<int>("Sector") == 1 || x.Field<int>("Sector") == 2).ToList();

Upvotes: 1

Related Questions