關於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;  
 }

  

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章