删除重复条目并合并Datatable中的所有列数据

时间:2014-12-12 11:36:43

标签: c# asp.net datatable

我在这个数据表中有数据。

id    antena  size  width  kpi1value kpi2value kpi3value erlvalue mbvalue kpi4value   
ab1     22     33    40      20                            22          
ab2     33     45    50      30                            30
ab3     11     40    60      20                            33
ab1     22     33    40                 55                           25
ab2     33     45    50                 30                           22
ab3     11     40    60                 20                           90
ab1     22     33    40                            40
ab2     33     45    50                            50
ab3     11     40    60                            20
ab1     22     33    40                                                         45
ab2     33     45    50                                                         55
ab3     11     40    60                                                         20

现在,我必须使用通用ID来安排这些数据。喜欢这个

 id    antena  size  width  kpi1value kpi2value kpi3value erlvalue mbvalue kpi4value   
ab1     22     33    40      20         55        40         22        25    45
ab2     33     45    50      30         30        50         30        22    55
ab3     11     40    60      20         20        20         33        90    20

我尝试使用DefaultView删除重复项。

dt = dt.DefaultView.ToTable(true)

但是,有了这个,我得到了所有专栏。

那么,我如何在数据表中安排这些数据?

1 个答案:

答案 0 :(得分:2)

我认为没有特殊的方法可以获得预期效果,但很难预测用户想要使用非唯一数据(等等,kpi1value,kpi2value)。我认为你很幸运,程序员喜欢重新发明轮子的过程,所以我编写了一些可能用于完成任务的代码。

代码

public DataTable SpecialDistinct(DataTable table, 
    IList<string> uniqueFields, IList<string> otherFields)
{
    // create a table for results
    var resultTable = table.Clone();

    var view = new DataView(table);
    var distinctTable = view.ToTable(true, uniqueFields.ToArray());

    foreach (DataRow distictRow in distinctTable.Rows)
    {
        var row = resultTable.NewRow();
        foreach (var uniqueField in uniqueFields)
        {
            row[uniqueField] = distictRow[uniqueField];
        }

        IEnumerable<DataRow> selectedRows = table.AsEnumerable().Where(
            r => uniqueFields.All(
            uniqueField => r[uniqueField].Equals(distictRow[uniqueField])));

        foreach (var otherField in otherFields)
        {
            var selectedRow = selectedRows.FirstOrDefault(
                r => r[otherField] != DBNull.Value);
            if (selectedRow != null)
                row[otherField] = selectedRow[otherField];
        }
        resultTable.Rows.Add(row);
    }

    return resultTable;
}

测试

[TestMethod]
public void SpecialDistinctTest()
{
    // create a table
    var table = new DataTable();
    var uniqueFields = new List<string> { "ID", "antenna", "size" };
    var otherFields = new List<string> { "kpi1value", "kpi2value" };

    foreach (var uniqueField in uniqueFields)
    {
        table.Columns.Add(new DataColumn(uniqueField, typeof(Int32)));
    }

    foreach (var otherField in otherFields)
    {
        table.Columns.Add(new DataColumn(otherField, typeof(Int32)));
    }

    // add some items.
    AddRows(table);

    DataTable resultTable = SpecialDistinct(table, uniqueFields, otherFields);

    // check results from the table table
}

private void AddRows(DataTable table)
{
    DataRow row = table.NewRow();
    row.ItemArray = new object[] { 22, 33, 40, 20, DBNull.Value };
    table.Rows.Add(row);

    row = table.NewRow();
    row.ItemArray = new object[] { 33, 45, 50, 30, DBNull.Value };
    table.Rows.Add(row);

    row = table.NewRow();
    row.ItemArray = new object[] { 22, 33, 40, DBNull.Value, 55 };
    table.Rows.Add(row);

    row = table.NewRow();
    row.ItemArray = new object[] { 33, 45, 50, DBNull.Value, 30 };
    table.Rows.Add(row);
}

结果

原始表格 enter image description here

结果表 enter image description here

相关问题