Fetching contributors…
Cannot retrieve contributors at this time
39 lines (27 sloc) 3.04 KB
title dev_langs ms.assetid
Comparing DataRows (LINQ to DataSet)

Comparing DataRows (LINQ to DataSet)

[!INCLUDEvbteclinqext] defines various set operators to compare source elements to see if they are equal. [!INCLUDEvbteclinq] provides the following set operators:

  • xref:System.Linq.Enumerable.Distinct%2A

  • xref:System.Linq.Enumerable.Union%2A

  • xref:System.Linq.Enumerable.Intersect%2A

  • xref:System.Linq.Enumerable.Except%2A

These operators compare source elements by calling the xref:System.Collections.Generic.IEqualityComparer%601.GetHashCode%2A and xref:System.Collections.Generic.IEqualityComparer%601.Equals%2A methods on each collection of elements. In the case of a xref:System.Data.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 xref:System.Data.DataRowComparer class has been added to [!INCLUDElinq_dataset]. This class can be used to compare row values.

The xref:System.Data.DataRowComparer class contains a value comparison implementation for xref:System.Data.DataRow, so this class can be used for set operations such as xref:System.Linq.Enumerable.Distinct%2A. This class cannot be directly instantiated; instead, the xref:System.Data.DataRowComparer.Default%2A property must be used to return an instance of the xref:System.Data.DataRowComparer%601. The xref:System.Data.DataRowComparer%601.Equals%2A method is then called and the two xref:System.Data.DataRow objects to be compared are passed in as input parameters. The xref:System.Data.DataRowComparer%601.Equals%2A method returns true if the ordered set of column values in both xref:System.Data.DataRow objects are equal; otherwise, false.


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

[!code-csharp[DP LINQ to DataSet Examples#Intersect2](../../../../samples/snippets/csharp/VS_Snippets_ADO.NET/DP LINQ to DataSet Examples/CS/Program.cs#intersect2)] [!code-vb[DP LINQ to DataSet Examples#Intersect2](../../../../samples/snippets/visualbasic/VS_Snippets_ADO.NET/DP LINQ to DataSet Examples/VB/Module1.vb#intersect2)]


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

[!code-vb[DP LINQ to DataSet Examples#CompareDifferentRows](../../../../samples/snippets/visualbasic/VS_Snippets_ADO.NET/DP LINQ to DataSet Examples/VB/Module1.vb#comparedifferentrows)]

See Also

Loading Data Into a DataSet
LINQ to DataSet Examples