C# 如果您的数据表的主键是两列,您可以使用 DataTable.Contains(object key) 吗?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/917622/
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
Can you use DataTable.Contains(object key) if your datatable's primary key is two columns?
提问by Slim
if so how?
如果是这样怎么办?
采纳答案by van
To select by a primary key you should use one of:
要通过主键进行选择,您应该使用以下选项之一:
DataTable.Rows.Find(Object)
in case your PK is one columnDataTable.Rows.Find(Object[])
in case you have more then 1 column as a primary key
DataTable.Rows.Find(Object)
如果你的 PK 是一列DataTable.Rows.Find(Object[])
如果您有超过 1 列作为主键
In case of a typed DataSet, the method MyDataTable.Rows.Find(...)
will be generated for you with the proper signature. Basically it is a method on DataRowCollection class
如果是类型化的 DataSet,MyDataTable.Rows.Find(...)
将使用正确的签名为您生成该方法。基本上它是 DataRowCollection 类上的一个方法
回答by Clyde
'Contains' does not seem to be a member of the DataRow class (maybe this is a typed data set?)
“包含”似乎不是 DataRow 类的成员(也许这是一个类型化数据集?)
In any case, you can always use (DataTable.Select(....).Length > 0) as a substitute
在任何情况下,您始终可以使用 (DataTable.Select(....).Length > 0) 作为替代
回答by Thomas Levesque
I assume you're referring to the DataRowCollection.Contains method ? There is an overload that takes an array of objects, you should use this one
我假设您指的是 DataRowCollection.Contains 方法?有一个需要一组对象的重载,你应该使用这个