Export (0) Print
Expand All

Comparing DataRows (LINQ to DataSet)

Language-Integrated Query (LINQ) defines various set operators to compare source elements to see if they are equal. LINQ provides the following set operators:

These operators compare source elements by calling the GetHashCode and Equals methods on each collection of elements. 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. 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. 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. This class cannot be directly instantiated; instead, the Default property must be used to return an instance of the DataRowComparer. The Equals(DataRow, DataRow) method is then called and the two DataRow objects to be compared are passed in as input parameters. The Equals(DataRow, DataRow) method returns true if the ordered set of column values in both DataRow objects are equal; otherwise, false.

This example uses Intersect to return contacts that appear in both tables.


// Fill the DataSet.
DataSet ds = new DataSet();
ds.Locale = CultureInfo.InvariantCulture;
FillDataSet(ds);

DataTable contactTable = ds.Tables["Contact"];

// Create two tables.
IEnumerable<DataRow> query1 = from contact in contactTable.AsEnumerable()
                              where contact.Field<string>("Title") == "Ms."
                              select contact;

IEnumerable<DataRow> query2 = from contact in contactTable.AsEnumerable()
                              where contact.Field<string>("FirstName") == "Sandra"
                              select contact;


DataTable contacts1 = query1.CopyToDataTable();
DataTable contacts2 = query2.CopyToDataTable();

// Find the intersection of the two tables.
var contacts = contacts1.AsEnumerable().Intersect(contacts2.AsEnumerable(),
                                                    DataRowComparer.Default);

Console.WriteLine("Intersection of contacts tables");
foreach (DataRow row in contacts)
{
    Console.WriteLine("Id: {0} {1} {2} {3}",
        row["ContactID"], row["Title"], row["FirstName"], row["LastName"]);
}


Example

The following example compares two rows and gets their hash codes.


        ' Fill the DataSet.
        Dim ds As New DataSet()
        ds.Locale = CultureInfo.InvariantCulture
        ' See the FillDataSet method in the Loading Data Into a DataSet topic.
        FillDataSet(ds)

        ' Get two rows from the SalesOrderHeader table.
        Dim table As DataTable = ds.Tables("SalesOrderHeader")
        Dim left = table.Rows(0)
        Dim right = table.Rows(1)

        ' Compare the two different rows.
        Dim comparer As IEqualityComparer(Of DataRow) = DataRowComparer.Default
        Dim bEqual = comparer.Equals(left, right)

        If (bEqual = True) Then
            Console.WriteLine("Two rows are equal")
        Else
            Console.WriteLine("Two rows are not equal")
        End If

        ' Output the hash codes of the two rows.
        Console.WriteLine("The hashcodes for the two rows are {0}, {1}", _
            comparer.GetHashCode(left), _
            comparer.GetHashCode(right))



Show:
© 2014 Microsoft