English 中文(简体)
比较两个数据表和格式结果
原标题:Compare two datatables and format results

我知道如何用斜体来做到这一点,但就c#.I而言,无法说明如何比较两个数据表。

Let s say:

第1号数据表:

Name  |  Balance | Description
Smith |   1200   | Smith owes 600
Jordan|   4000   | Hi Jordan
Brooks|   5000   | I like my cat
Navaro|   6000   | description here
Gates |   9010   | omg

2nd datatable:

Name  |  Balance | Description
Smith |   1600   | Smith owes 600
Jordan|   4200   | I M JORDAN
Clay  |   9000   | Test description
Brooks|   5000   | I like my cat

I want to dump results of comparison to a simple html table.

Soooo...result should be like this:
enter image description here

基本上,我需要的是:

  1. 显示栏目不同,显示数据

  2. 如果所有栏目相同,不作记录

  3. 只存在于第一个数据表(合理名称)中的显示记录

  4. 仅存在于第二数据表(合理名称)中的显示记录

在 s中,你可以做像合并一样的事情,然后是 p。

But in C#, my findings: I can use except, or intersect, but it returns one dattable. Is there any formatting options for an exceptintersection functions?

我期待就如何最好地实现这一目标提出建议。 (每个数据表中约有100列)。 两者都应按名称加以比较。

最佳回答

这里是你在档案中需要使用的。

(我只创造了这两个空档,以避免在代码<代码>上出现。) 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!

问题回答

暂无回答




相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

How to Add script codes before the </body> tag ASP.NET

Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...

Transaction handling with TransactionScope

I am implementing Transaction using TransactionScope with the help this MSDN article http://msdn.microsoft.com/en-us/library/system.transactions.transactionscope.aspx I just want to confirm that is ...

System.Web.Mvc.Controller Initialize

i have the following base controller... public class BaseController : Controller { protected override void Initialize(System.Web.Routing.RequestContext requestContext) { if (...

Microsoft.Contracts namespace

For what it is necessary Microsoft.Contracts namespace in asp.net? I mean, in what cases I could write using Microsoft.Contracts;?

Separator line in ASP.NET

I d like to add a simple separator line in an aspx web form. Does anyone know how? It sounds easy enough, but still I can t manage to find how to do it.. 10x!

热门标签