关于DataTable动态列名拼接

 /// <summary>
 /// 多条件的过滤返回DataTable 
 /// </summary>
 /// <param name="dataTable"></param>
 /// <param name="filterConditions"></param>
 /// <returns></returns>
 private DataTable  MuilteExpressionFilter(DataTable dataTable,List<dynamic> filterConditions)
 {
     var filteredRows = dataTable.AsEnumerable()
 .Where(row =>
 {
     foreach (dynamic condition in filterConditions)
     {
         if (condition.Key != null && condition.Value != null)
         {
             string fieldName = condition.Key.ToString();
             string fieldValue = condition.Value.ToString();
             if (row[fieldName].ToString() != fieldValue)
             {
                 return false;
             }
         }
     }
     return true;
 });

     // 创建新 DataTable 存储过滤结果
     DataTable filteredDataTable = dataTable.Clone();
     foreach (DataRow filteredRow in filteredRows)
     {
         filteredDataTable.ImportRow(filteredRow);
     }

     return  filteredDataTable;  
 }

  

posted @ 2024-04-25 13:47  LowKeyC  阅读(10)  评论(0编辑  收藏  举报
有志者事竟成破釜沉舟百二秦关终属楚苦心人,天不负,卧薪尝胆,三千越甲可吞吴