这里是你在档案中需要使用的。
(我只创造了这两个空档,以避免在代码<代码>上出现。) Dictionary<object, Dictionary<string, Tuple<object,bject>>>,但如果你更愿意,你可以取代。
protected class Differences : Dictionary<object, RowDifferences>
{
}
protected class RowDifferences : Dictionary<string, Tuple<object, object>>
{
}
protected Differences GetDifferences(DataTable table1,
DataTable table2,
out IEnumerable<object> onlyIn1,
out IEnumerable<object> onlyIn2)
{
var arr1 = new DataRow[table1.Rows.Count];
var arr2 = new DataRow[table2.Rows.Count];
table1.Rows.CopyTo(arr1, 0);
table2.Rows.CopyTo(arr2, 0);
onlyIn1 = arr1.Where(x1 => arr2.All(x2 => x1[0] != x2[0])).Select(dr => dr[0]);
onlyIn2 = arr2.Where(x1 => arr1.All(x2 => x1[0] != x2[0])).Select(dr => dr[0]);
var differences = new Differences();
foreach (var x1 in arr1)
{
foreach (var x2 in arr2)
{
if (x1[0] == x2[0])
{
var rowDifferences = new RowDifferences();
for (var i = 1; i < x1.ItemArray.Length; i++)
{
if (x1.ItemArray[i] != x2.ItemArray[i])
{
rowDifferences.Add(table1.Columns[i].ColumnName,
new Tuple<object, object>(x1.ItemArray[i], x2.ItemArray[i]));
}
}
differences.Add(x1[0], rowDifferences);
}
}
}
return differences;
}
protected void GenerateTables(out DataTable table1, out DataTable table2)
{
table1 = new DataTable();
table2 = new DataTable();
table1.Columns.Add("Name");
table1.Columns.Add("Balance");
table1.Columns.Add("Description");
table2.Columns.Add("Name");
table2.Columns.Add("Balance");
table2.Columns.Add("Description");
table1.Rows.Add("Smith", 1200, "Smith owes 600");
table1.Rows.Add("Jordan", 4000, "Hi Jordan");
table1.Rows.Add("Brooks", 5000, "I like my cat");
table1.Rows.Add("Navaro", 6000, "description here");
table1.Rows.Add("Gates", 9010, "omg");
table2.Rows.Add("Smith", 1600, "Smith owes 600");
table2.Rows.Add("Jordan", 4200, "I M JORDAN");
table2.Rows.Add("Clay", 9000, "Test description");
table2.Rows.Add("Brooks", 5000, "I like my cat");
}
这里是如何将表格放在.aspx档案中的例子:
<%
DataTable table1, table2;
GenerateTables(out table1, out table2);
IEnumerable<object> onlyIn1, onlyIn2;
var differences = GetDifferences(table1, table2, out onlyIn1, out onlyIn2);
%>
<table>
<thead>
<tr>
<th>Name</th>
<th>RecordName</th>
<th>1st Datatable</th>
<th>2nd Datatable</th>
</tr>
</thead>
<tbody>
<%
foreach (var difference in differences)
{
%>
<tr>
<td><%=difference.Key%></td>
</tr>
<%
foreach (var rowDifferences in difference.Value)
{
%>
<tr>
<td></td>
<td><%=rowDifferences.Key%></td>
<td><%=rowDifferences.Value.Item1%></td>
<td><%=rowDifferences.Value.Item2%></td>
</tr>
<%
}
}
%>
<tr>
<td>Only 1st datatable</td>
</tr>
<%
foreach (var name in onlyIn1)
{
%>
<tr>
<td><%=name%></td>
</tr>
<%
}
%>
<tr>
<td>Only 2st datatable</td>
</tr>
<%
foreach (var name in onlyIn2)
{
%>
<tr>
<td><%=name%></td>
</tr>
<%
}
%>
</tbody>
</table>
如你所希望,坐在桌旁是很难的。
因此,你留下的主要事情是将<代码>GenerateTables改为某些令人怀疑的逻辑,或甚至放在内。
调查结果算法或许可以完善。 在最坏的情况下,目前是O(m* n *k),m和n是表1和表2中各行数,k是栏数。 我本可以想办法改进,但我留给你。 这应当使你们开始ice。
Just note that this algorithm assumes that the columns are equal between the two tables.
让我知道,是否对解决办法有任何不明确之处,好uck!