vb.net .Net 添加索引到数据表(数据集)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/37016084/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
.Net Add Index to Datatable (dataset)
提问by Jeff
Is there a way to add an index to a datatable in .Net?
有没有办法向 .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,100 行的数据表,并且 datatable.select 语句对于内存操作来说似乎有点慢。
采纳答案by F0r3v3r-A-N00b
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.
这将返回一个数据行。另外主键值必须唯一,否则会抛出异常。
回答by Kusal Dissanayake
For Visual Basic:
对于 Visual Basic:
Dim table As New DataTable()
table.Columns.Add(New DataColumn("MyColumn"))
Dim primaryKey(1) As DataColumn
primaryKey(1) = table.Columns("MyColumn")
table.PrimaryKey = primaryKey

