简体   繁体   English

在DataRow集合上调用Distinct()

[英]Calling Distinct() on DataRow collection

Trying to find unique rows in a data table using Distinct() extension method. 尝试使用Distinct()扩展方法在数据表中查找唯一行。 Some rows contain exactly the same data, but for some reason, the hash code for these rows are different from each other. 某些行包含完全相同的数据,但是由于某些原因,这些行的哈希码彼此不同。

I wrote a comparer class implementing IEqualityComparer<DataRow> , however, I think what I'm doing in GetHashCode() is cheesy and nasty. 我编写了一个实现IEqualityComparer<DataRow>的比较器类,但是,我认为我在GetHashCode()所做的事情既俗气又讨厌。

The reason I've done it this way is because Equals() never gets called unless the hashcodes are the same (Expected behaviour) 我这样做的原因是,除非哈希码相同,否则Equals()永远不会被调用(预期行为)

class RowValidationComparer : IEqualityComparer<DataRow>
        {
            public bool Equals(DataRow x, DataRow y)
            {
                return x.Field<string>("MyField").Equals(y.Field<string>("MyField"));
            }

            public int GetHashCode(DataRow obj)
            {
                typeof(DataRow).GetHashCode();
            }
        }

Trying to find unique rows in a data table using Distinct() extension method. 尝试使用Distinct()扩展方法在数据表中查找唯一行。

To do this, you can use the DataRowComparer class: 为此,可以使用DataRowComparer类:

var distinct = dataTable.AsEnumerable().Distinct(DataRowComparer.Default);

For a general explanation from MSDN , discussing the use of set operators such as Distinct on DataRows: 对于MSDN的一般性解释,讨论了在DataRows上使用Distinct之类的集合运算符的用法:

These operators compare source elements by calling the GetHashCode and Equals methods on each collection of elements. 这些运算符通过在每个元素集合上调用GetHashCode和Equals方法来比较源元素。 In the case of a DataRow, these operators perform a reference comparison, which is generally not the ideal behavior for set operations over tabular data. 对于DataRow,这些运算符执行参考比较,这通常不是对表格数据进行设置操作的理想行为。 For set operations, you usually want to determine whether the element values are equal and not the element references. 对于设置操作,通常需要确定元素值是否相等,而不是元素引用。 Therefore, the DataRowComparer class has been added to LINQ to DataSet. 因此,DataRowComparer类已被添加到LINQ到DataSet中。 This class can be used to compare row values. 此类可用于比较行值。

The DataRowComparer class contains a value comparison implementation for DataRow, so this class can be used for set operations such as Distinct. DataRowComparer类包含DataRow的值比较实现,因此此类可用于诸如Distinct之类的设置操作。

You might try ... 您可以尝试...

public int GetHashCode(DataRow obj) {
    return obj.Field<string>("MyField").GetHashCode();
}

It becomes more complicated the more fields you add to the hash code value. 添加到哈希码值中的字段越多,它就会变得越复杂。 Also, you may want to add null reference checks. 另外,您可能要添加空引用检查。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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