简体   繁体   中英

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

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";
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));

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");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM