将Datatable重复项与字典组合在一起,并解决重复的数据表数据问题

时间:2016-01-11 21:14:28

标签: c# .net dictionary datatable datarow

我确实有另一个问题,我认为我在路上只是意识到字典很有用,但我需要能够真正使用我的数据表来循环。

字典正确显示数据,但我对已编辑数据表的循环清楚地显示了已编辑和旧数据

https://dotnetfiddle.net/6BzsYh

    DataTable table1 = new DataTable();
    table1.Columns.Add("ID", typeof (int));
    table1.Columns.Add("Weight", typeof (int));
    table1.Columns.Add("Name", typeof (string));
    table1.Columns.Add("Breed", typeof (string));
    table1.Rows.Add(23, 57, "Koko", string.Empty);
    table1.Rows.Add(44, 130, "Fido", null);
    table1.Rows.Add(54, 130, "Jack", null);
    table1.Rows.Add(44, 130, "Thad", null);
    table1.Rows.Add(64, 130, "John", null);
    table1.Rows.Add(23, 130, "Brian", null);
    table1.Rows.Add(445, 130, "James", null);
    table1.Rows.Add(64, 134, "Adam", null);

    Dictionary<int, string> dict = new Dictionary<int, string>();

    for (int i = 0; i < table1.Rows.Count; i++)
    {
        int id = Convert.ToInt32(table1.Rows[i][0]);
        if (dict.ContainsKey(id))
        {
            //comma separate the Names
            dict[id] += ", " + table1.Rows[i][2].ToString();
            // change the Name value in the table
            table1.Rows[i][2] = dict[id];
        //Console.WriteLine(dict[id]);
        }
        else
        {
            dict.Add(id, table1.Rows[i][2].ToString());
        }
    //Console.WriteLine()
    }

    foreach (DataRow eaRow in table1.Rows)
    {
        Console.WriteLine("ID=" + eaRow[0] + "  Name=" + eaRow[2]);
    }

    Console.WriteLine("\n\n");
    //dictionary is correct
    foreach (var item in dict)
    {
        Console.WriteLine("ID=" + item.Key + " Name=" + item.Value);
    }

2 个答案:

答案 0 :(得分:1)

如果您需要重复值列表,则需要执行以下操作 我还没有在Range of Datatable字段上尝试过这个,所以你需要用ID字段和Weight字段的2个不同字段来测试它。

    DataTable table11 = new DataTable();
    table11.Columns.Add("ID", typeof(int));
    table11.Columns.Add("Weight", typeof(int));
    table11.Columns.Add("Name", typeof(string));
    table11.Columns.Add("Breed", typeof(string));
    table11.Rows.Add(23, 57, "Koko", string.Empty);
    table11.Rows.Add(44, 130, "Fido", null);
    table11.Rows.Add(54, 130, "Jack", null);
    table11.Rows.Add(44, 130, "Thad", null);
    table11.Rows.Add(64, 130, "John", null);
    table11.Rows.Add(23, 130, "Brian", null);
    table11.Rows.Add(445, 130, "James", null);
    table11.Rows.Add(64, 134, "Adam", null);

   var duplicate = table1.AsEnumerable()
       .Select(dr => dr.Field<int>("ID"))
       .GroupBy(x => x)
       .Where(g => g.Count() > 1)
       .Select(g => g.Key)
       .ToList();

   var duplicate2 = table1.AsEnumerable()
       .Select(dr => dr.Field<int>("Weight"))
       .GroupBy(x => x)
       .Where(g => g.Count() > 1)
       .Select(g => g.Key)
       .ToList();

如果要从数据表中删除重复项,请参阅此stackoverflow发布

Best way to remove duplicate entries from a data table

答案 1 :(得分:1)

好的,那么这样做,请注意,如果你想保留所有其他列的值,那么你需要检查那些,请注意我在那里的存根被注释掉的&#34;其他&#34;

专业人员:完全控制您的数据

缺点:不是动态的,有点时间添加所有列

DataTable dtResult = table1.Clone(); // This will be empty at first

var distinctRows = table1.DefaultView.ToTable(true, "ID").Rows.OfType<DataRow>().Select(k => k[0] + "").ToArray();

       foreach (string id in distinctRows)
       {
           var rows = table1.Select("ID= '" + id + "'");
           string Name = "";
           //string other = "";
           foreach (DataRow row in rows)
           {

               Name+= row["Name"] + ",";
              // other += row["other"];
           }
           Name = Name.Trim(',');

           dtResult.Rows.Add(ID, Name)  // other);
           Name = "";
           //other = "";


       }

       //var output = dtResult; 



        foreach(DataRow dr in dtResult.Rows)
        {
            Console.WriteLine(dr[0] + " --- " + dr[1]); // + "--- " + dr[2]);
        }