删除特定数据表的行

时间:2016-09-26 13:59:25

标签: c# dictionary datatable

我填写了一本字典,父母作为关键字,他们的孩子作为价值List<>来自DataTable关系,效果非常好,每个孩子都被保存为结构名为Definition并插入List<Definition>

 structure Definition
  {
     int id; 
     int atom; 
     int id_d
  };

但我想要实现的是在迭代Dictionary<Definitions,List<Definitions>>上的值列表时,我的意思是DataRelation的孩子,我想要删除DataTable个真表中的某些行表示正在迭代List<Definitions>Dictionary的值 即

Dictionary<Definitions,List<Definitions>> dataRelation;
DataTable parents;
DataTabale children; 

 foreach(var it in dataRelation)
 {
     for(int i =0 ; i< it.value.count : i++)
     {
          if(is the row i want to delete)
          {
               Children.delete(with the structure's Definition value parameters )
          }

     }
 }

结构的Definitions变量与DataTabale colum的名称相同。

1 个答案:

答案 0 :(得分:0)

我必须承认我不知道问题是什么,但我会使用这种可读的方法:

var rowsToDelete = from cRow in children.AsEnumerable()
                   join  r in dataRelation.Values
                   on new { 
                      id   = cRow.Field<int>("id"), 
                      atom = cRow.Field<int>("atom"), 
                      id_d = cRow.Field<int>("id_d") 
                   } equals new { r.id, r.atom, r.id_d }
                   select cRow;
foreach(DataRow rowToDelete in rowsToDelete)
    rowToDelete.Delete();

// now use a dataadapter to actually delete this row from the datasource