Is there a way to achieve the same below output without using for/each loop? (using lambda expression?)
DataRow leftDataRow = GetReferenceDataRow();
DataTable table = GetTableForComparison():
List<NonMatchingValue> listColumnsWithDiscrepancies = new List<NonMatchingValue>();
foreach (DataRow row in table.Rows)
{
for (int index = 0; index < table.Columns.Count; index++)
{
if (leftDataRow[index].ToString() != row[index].ToString())
{
listColumnsWithDiscrepancies.Add(new NonMatchingValue {
FieldSource = leftDataRow.Tables.Columns[index].ColumnName,
FieldTarget = row.Table.Columns[index].ColumnName
ValueSource = leftDataRow[index].ToString(),
ValueTarget = row[index].ToString(),
IsEqual = leftDataRow[index].Equal(row[index])
});
}
}
}
The structure/number of columns of leftDataRow and rows in the table is identical.
// Sample Output:
FieldSource | FieldTarget | ValueSource | ValueTarget | IsEqual
-------------------------------------------------------------------------------
Column1 | Column1 | X | Y | False
Column2 | Column2 | A | A | True