boingboing
boingboing

Reputation:

performing a select on a c# datatable

I have a datatable with a bunch of rows in it, the first column is an Int32 and I want to perform a simple select like:

select * from MyDataTable where column1 = 234

Upvotes: 3

Views: 872

Answers (3)

Mehrdad Afshari
Mehrdad Afshari

Reputation: 421968

var result = from row in table.AsEnumerable()
             where row[0].Equals(42)
             select row;

Or

var result = table.AsEnumerable().Where(row => row[0].Equals(42));

Upvotes: 2

Canavar
Canavar

Reputation: 48088

Try this to get result as row array :

DataRow[] rows = myDataTable.Select("column1 = 234");

Or this to get dataview :

DataView myDataView = myDataTable.DefaultView;
myDataView.RowFilter = "column1 = 234";

Upvotes: 6

Jhonny D. Cano -Leftware-
Jhonny D. Cano -Leftware-

Reputation: 18013

if you're talking of a System.Data.DataTable, you can use datatable.Rows.Find for searching a row by primaryKey, or datatable.Select for obtaining a array of rows that satisfy your condition.

// By DataTable's primary key

datatable.Rows.Find(234);

// By compound primary key

datatable.Rows.Find(234, 1, 4);

// by Select

datatable.Select("column1=234");

// by Compound Select

datatable.Select("column1=234 AND column2=1");

Upvotes: 1

Related Questions