将可数据复制的项目与字典结合起来,并获得重复的数据表数据的问题 [英] Combining Datatable duplicated items with a dictionary and getting issue with duplicate datatable data

查看:155
本文介绍了将可数据复制的项目与字典结合起来,并获得重复的数据表数据的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我确实有另一个问题,我认为我的方式只是意识到该字典是非常有用的,但我需要能够真正使用我的Datatable循环。



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



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);

字典< 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))
{
//逗号分开名称
dict [id] + =,+ table1.Rows [i] [2 ] .ToString();
//更改表中的Name值
table1.Rows [i] [2] = dict [id];
//Console.WriteLine(dict[id]);
}
else
{
dict.Add(id,table1.Rows [i] [2] .ToString());
}
//Console.WriteLine()
}

foreach(Table1.Rows中的DataRow eaRow)
{
Console.WriteLine (ID =+ eaRow [0] +Name =+ eaRow [2]);
}

Console.WriteLine(\\\
\\\
);
//字典是正确的
foreach(dict中的var项目)
{
Console.WriteLine(ID =+ item.Key +Name =+ item.Value );
}


解决方案

注意,如果您想要保留所有其他列值,那么您需要先放在其中,注意我已经在那里被注释掉其他的stubbed的一个



专业版:完全控制您的数据



cons :不动态,有一段时间添加所有您的列

  DataTable dtResult = table1.Clone(); //这首先是空的

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

foreach(distinctRows中的字符串ID)
{
var rows = table1.Select(ID ='+ id +');
string Name =;
// string other =;
foreach(行中的DataRow行)
{

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

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


}

// var output = dtResult;



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


I did have another question in which I thought I was well on my way only to realize that the Dictionary is of great use, but I need to be able to really use my Datatable to loop through.

The Dictionary is correctly showing the data, but my loop over the edited datatable is clearly showing both the edited and old data

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);
    }

解决方案

Ok so do this then , note if you are wanting to preserve ALL the other columns values then you need to plumb those in , notice the stubbed one that I have in there that is commented out "other"

pros: complete control of your data

cons: not dynamic , and a bit of time to add all your columns

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]);
        }

这篇关于将可数据复制的项目与字典结合起来,并获得重复的数据表数据的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆