.Net Add Index to Datatable (dataset)

2019-07-14 20:34发布

问题:

Is there a way to add an index to a datatable in .Net?

I have a datatable holding about 1,100 rows and the datatable.select statement seems a bit slow for an in-memory operation.

回答1:

table.PrimaryKey = new DataColumn[]{table.Columns("column1"),table.Columns("column2")}

when searching

table.Rows.Find(New Object(){<value in column1>, <value in column2>})

This will return a datarow. Also the Primary key values must be unique, otherwise an exception will be thrown.