Dictionary 和 DataTable 相关.要求性能。求大神!

Iam太陽神 2013-09-17 09:28:21
Dictionary 数据
1 A
2 B
3 C
.....
DataTable 数据
01 1 90
02 2 75
03 2 75
04 1 85
.........................
------------------------------------------
需要得到以下新数据:

New DataTable
01 A 90
02 B 75
03 B 75
04 A 85

...全文
239 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
Iam太陽神 2013-09-17
  • 打赏
  • 举报
回复
解决了。 在元表追加一新列B。 创建一个新表。元表克隆到新表。 再把A列数据复制到B列.

部分代码:
 Dictionary<int, DictDetail> repair_status = ClientPackage.GetDictionary("RepairCauseReason");

 //Dictionary数据最少,让其做最外层循环,比较优
            foreach (KeyValuePair<int, DictDetail> keyValue in repair_status)
            {
                //找出一组key的相关值,内层循环最少化 
                DataRow[] DrSelect = dt.Select("CAUSE_REASON = " + keyValue.Key);
                if (DrSelect != null && DrSelect.Length > 0)
                {
                    for (int i = 0; i < DrSelect.Length; i++)
                    {
                        DrSelect[i]["NEW_CAUSE_REASON"] = DrSelect[i]["CAUSE_REASON"];
                        DrSelect[i]["NEW_CAUSE_REASON"] = keyValue.Value.DetailName;
                        //修正后 DataRow保存进NewDataTable
                        NewDataTable.ImportRow(DrSelect[i]);
                    }
                }
            }
全栈极简 2013-09-17
  • 打赏
  • 举报
回复
那你就循环构造一个新的DataTable,用dictionary[key]找到相应的值替换即可。
Iam太陽神 2013-09-17
  • 打赏
  • 举报
回复
引用 1 楼 guwei4037 的回复:
static void Main(string[] args)
        {
            Dictionary<int, string> dictionary = new Dictionary<int, string>();
            dictionary.Add(1, "A");
           ...................
        }
大神,可以不用Linq吗?
全栈极简 2013-09-17
  • 打赏
  • 举报
回复
static void Main(string[] args)
        {
            Dictionary<int, string> dictionary = new Dictionary<int, string>();
            dictionary.Add(1, "A");
            dictionary.Add(2, "B");
            dictionary.Add(3, "C");

            DataTable dt = new DataTable();
            dt.Columns.Add("id", typeof(string));
            dt.Columns.Add("num", typeof(int));
            dt.Columns.Add("data", typeof(string));

            DataRow dr = null;
            dr = dt.NewRow();
            dr["id"] = "01";
            dr["num"] = 1;
            dr["data"] = "90";
            dt.Rows.Add(dr);

            dr = dt.NewRow();
            dr["id"] = "02";
            dr["num"] = 2;
            dr["data"] = "75";
            dt.Rows.Add(dr);

            dr = dt.NewRow();
            dr["id"] = "03";
            dr["num"] = 2;
            dr["data"] = "75";
            dt.Rows.Add(dr);

            dr = dt.NewRow();
            dr["id"] = "04";
            dr["num"] = 1;
            dr["data"] = "85";
            dt.Rows.Add(dr);

            var result = from p in dt.AsEnumerable()
                         from q in dictionary.AsEnumerable()
                         where q.Key == p.Field<int>("num")
                         select new { ID = p.Field<string>("id"), q.Value, DATA = p.Field<string>("data") };
            result.ToList().ForEach(x => Console.WriteLine(x.ID + "-" + x.Value + "-" + x.DATA));
        }

110,534

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧