【问题标题】:Remove columns from DataTable which are not in List<string>从 DataTable 中删除不在 List<string> 中的列
【发布时间】:2019-09-23 06:25:55
【问题描述】:

我们有这个List&lt;string&gt;

List<string> A = ['a','b','c']

还有一个Datatable [B] 与以下列

'a' 'b' 'c' 'd' 'e' // the columns of DataTable B

我们如何从Datatable B删除List&lt;string&gt; A 中所有未找到的列?

【问题讨论】:

  • 两个列表的交集。 stackoverflow.com/a/7188030/2845389
  • @Kaushik 我无法从答案中理解如何实现这一点......你能具体举一个字符串和数据表列表的例子吗?

标签: c# list


【解决方案1】:

如果您只想删除列表“A”中未找到的所有列

var A = new List<string> { "a", "b", "c" };
var toRemove = dt.Columns.Cast<DataColumn>().Select(x => x.ColumnName).Except(A).ToList();

foreach (var col in toRemove) dt.Columns.Remove(col);

【讨论】:

  • 谢谢,我收到“集合已修改;枚举操作可能无法执行”错误...
  • @levi 尝试添加.ToList() 复制值,将更新答案
【解决方案2】:

我建议一个简单的for 循环:

  DataTable table = new DataTable();

  table.Columns.Add("a", typeof(string));
  table.Columns.Add("b", typeof(string));
  table.Columns.Add("C", typeof(string));
  table.Columns.Add("d", typeof(string));
  table.Columns.Add("e", typeof(string));
  table.Columns.Add("F", typeof(string));

  var A = new List<string> { "a", "b", "c" };

  // HashSet is a better collection in the context:
  //  1. We can specify comparer (e.g. we can ignore case)
  //  2. It's faster if the collection has many items
  // Sure, you can put A.Contains instead of columnsToKeep.Contains
  HashSet<string> columnsToKeep = 
    new HashSet<string>(A, StringComparer.OrdinalIgnoreCase);

  // For i-th column we should either either keep column (do nothing) or remove it
  for (int i = table.Columns.Count - 1; i >= 0; --i)
    if (!columnsToKeep.Contains(table.Columns[i].ColumnName))
      table.Columns.RemoveAt(i);

【讨论】:

    【解决方案3】:

    你可以这样做

    int numCols = dt.Columns.Count;
    
    foreach(String str in A){//loop all strings in arrays
    
     for(int i=0;i<numCols;i++){//loop all columnames in dataTable B
          string columnName =dt.Columns[i].ColumnName.ToString();//get column Name
          //check if columnNames in B are equal to 
          if(!str.Equals(columnName){
            //remove column
             dt.Columns.RemoveAt(i);
          }else{
           //column name is equal to the string array
          }
       }
    }
    

    【讨论】:

    • 我已经编辑了我的答案,请随时接近并给出输出
    • 我认为应该是 string columnName = dt.Columns[i].ColumnName.ToString()
    【解决方案4】:

    你可以试试下面的代码。

     List<string> A = new List<string>() { "a", "b", "c" };
    
                DataTable dt = new DataTable();
                dt.Columns.Add("a");
                dt.Columns.Add("b");
                dt.Columns.Add("c");
                dt.Columns.Add("d");
                dt.Columns.Add("e");
                foreach (string col in A)
                {
                    dt.Columns.Remove(col);
                }
                foreach (DataColumn column in dt.Columns)
                {
                    WriteLine(column.ColumnName);
                    WriteLine(" ");
                }
    

    【讨论】:

    • 问题不是如何删除列表(a)中的列,而是如何只保留它们......
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-09-09
    • 2014-07-20
    • 1970-01-01
    • 1970-01-01
    • 2019-04-06
    相关资源
    最近更新 更多