这个答案不使用动态表达式。但是,正是我想出的解决方案给了我正在寻找的答案。
var sql = table.SqlSelect;
var dataLegacy = GetData(connections.Legacy, sql, table.TableName);
var dataUpdate = GetData(connections.Update, sql, table.TableName);
var compare = new List<CompareSet>();
var jsonColumns = table.Json.ToList() ?? null;
dataLegacy.AsEnumerable().ToList()
.ForEach(legacyRow =>
{
DataRow updateRow = default(DataRow);
if (table.KeyColumn.Any())
{
var keys = string.Join("|", table.KeyColumn.Select(kc => $"{legacyRow.Field<object>(kc)}"));
updateRow = dataUpdate.AsEnumerable()
.FirstOrDefault(y => keys == string.Join("|", table.KeyColumn.Select(kc => $"{y.Field<object>(kc)}")));
}
if (updateRow == null || JsonConvert.SerializeObject(legacyRow.ItemArray) != JsonConvert.SerializeObject(updateRow.ItemArray))
compare.Add(new CompareSet(compare.Count, legacyRow, updateRow, jsonColumns));
});
dataUpdate.AsEnumerable().ToList()
.ForEach(updateRow =>
{
DataRow legacyRow = default(DataRow);
if (table.KeyColumn.Any())
{
var keys = string.Join("|", table.KeyColumn.Select(kc => $"{updateRow.Field<object>(kc)}"));
legacyRow = dataLegacy.AsEnumerable()
.FirstOrDefault(y => keys == string.Join("|", table.KeyColumn.Select(kc => $"{y.Field<object>(kc)}")));
}
if (legacyRow == null)
compare.Add(new CompareSet(compare.Count, legacyRow, updateRow, jsonColumns));
});
if (compare.Any())
{
compare.OrderBy(c => c.OrderBy)
.Dump($"{table.SchemaTable} ( {compare.Count()} / {dataLegacy.Rows.Count} / {dataUpdate.Rows.Count} rows )", TableDumpDepth);
}
else
{
"No differences found in data".Dump($"{table.SchemaTable} ( 0 / {dataLegacy.Rows.Count} / {dataUpdate.Rows.Count} rows )");
}