using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
class Program
{
static List<decimal> GetDecimals(object[] array, decimal multiplier)
{
// Add all decimals in the row to a List of decimals.
// ... And return the list.
var list = new List<decimal>();
foreach (object value in array)
{
try
{
decimal result = (decimal)value;
result *= multiplier;
list.Add(result);
}
catch
{
// Error.
}
}
return list;
}
static void CompareRows(DataTable table1, DataTable table2)
{
foreach (DataRow row1 in table1.Rows)
{
foreach (DataRow row2 in table2.Rows)
{
// Get decimals in each row.
// ... Multiply items in row 1 by 1.5.
var decimals1 = GetDecimals(row1.ItemArray, (decimal)1.5);
var decimals2 = GetDecimals(row2.ItemArray, (decimal)1.0);
// See if the row is valid.
if (decimals1.SequenceEqual(decimals2))
{
Console.WriteLine(
"OK: " + decimals1[0]);
}
else
{
Console.WriteLine(
"NOT OK: " + decimals1[0]);
}
}
}
}
static DataTable GetTable1()
{
var table = new DataTable { TableName =
"Prices in Sterling" };
table.Columns.Add(
"Product", typeof(string));
table.Columns.Add(
"Variety1", typeof(decimal));
table.Columns.Add(
"Variety2", typeof(decimal));
table.Columns.Add(
"Variety3", typeof(decimal));
table.Columns.Add(
"Variety4", typeof(decimal));
table.Rows.Add(
"Product 1", 10, 12, 14, 45);
table.Rows.Add(
"Product 2", 20, 15, 24, null);
table.Rows.Add(
"Product 3", 22, 60, null, null);
table.Rows.Add(
"Product 4", 28, null, null, null);
var sum1 = (decimal)table.Compute(
"SUM(Variety1)", string.Empty);
var sum2 = (decimal)table.Compute(
"SUM(Variety2)", string.Empty);
var sum3 = (decimal)table.Compute(
"SUM(Variety3)", string.Empty);
var sum4 = (decimal)table.Compute(
"SUM(Variety4)", string.Empty);
table.Rows.Add(
"Total", sum1, sum2, sum3, sum4);
return table;
}
static DataTable GetTable2()
{
var table = new DataTable { TableName =
"Prices in Euro" };
table.Columns.Add(
"Product", typeof(string));
table.Columns.Add(
"Variety1", typeof(decimal));
table.Columns.Add(
"Variety2", typeof(decimal));
table.Columns.Add(
"Variety3", typeof(decimal));
table.Columns.Add(
"Variety4", typeof(decimal));
table.Rows.Add(
"Product 1", 15, 18, 21, 67.5);
table.Rows.Add(
"Product 2", 30, 22.5, 36, null);
table.Rows.Add(
"Product 3", 33, 90, null, null);
table.Rows.Add(
"Product 4", 42, null, 24, null);
decimal sum1 = (decimal)table.Compute(
"SUM(Variety1)", string.Empty);
decimal sum2 = (decimal)table.Compute(
"SUM(Variety2)", string.Empty);
decimal sum3 = (decimal)table.Compute(
"SUM(Variety3)", string.Empty);
decimal sum4 = (decimal)table.Compute(
"SUM(Variety4)", string.Empty);
table.Rows.Add(
"Total", sum1, sum2, sum3, sum4);
return table;
}
static void Main()
{
CompareRows(GetTable1(), GetTable2());
}
}
OK: 15.0
NOT OK: 15.0
NOT OK: 15.0
NOT OK: 15.0
NOT OK: 15.0
NOT OK: 30.0
OK: 30.0
NOT OK: 30.0
NOT OK: 30.0
NOT OK: 30.0
NOT OK: 33.0
NOT OK: 33.0
OK: 33.0
NOT OK: 33.0
NOT OK: 33.0
NOT OK: 42.0
NOT OK: 42.0
NOT OK: 42.0
NOT OK: 42.0
NOT OK: 42.0
NOT OK: 120.0
NOT OK: 120.0
NOT OK: 120.0
NOT OK: 120.0
NOT OK: 120.0