2

可能重复:
如何从数据表中选择不同的值?

这是我目前正在处理的代码。我试图摆脱我的数据集中的重复字段。我相信这样做的正确方法是使用 If 语句。有人可以帮忙吗?这是我到目前为止的代码:

4

2 回答 2

2

There is this DataView method called ToTable with two parameters: (and a three-parameter overloaded version)

a boolean param distinct If true, the returned System.Data.DataTable contains rows that have distinct values for all its columns. The default value is false.

a string array param columnNames A string array that contains a list of the column names to be included in the returned System.Data.DataTable. The System.Data.DataTable contains the specified columns in the order they appear within this array.

// create a dv from the source dt 
 DataView dv = new DataView(dt); 
// set the output columns array of the destination dt 
 string[] strColumns = {"NodeID", "Title", "Url"}; 
// true = yes, i need distinct values. 
 dt = dv.ToTable(true, strColumns);

reference : Remove Duplicate Records in a DataTable the Easy Way

于 2012-11-20T17:11:40.073 回答
0

每次我需要唯一性时,我都会使用 HashSet 而不是其他结构。所以简短的回答是:我不知道。

Hashset 有一个很好的特性,可以让你决定它认为什么是独特的——只需使用适当的构造函数。

另一件事是 - 如果您不使用 DataSets 来访问数据库 - 不要使用它,如果您这样做了,那么请改用强类型版本。如果您有充分的理由这样做,这当然不适用——这只是一般规则。

于 2012-11-20T17:22:23.027 回答