关于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;
}
再牛逼的梦想,也抵不住我傻逼似的坚持!别在该奋斗的年纪,贪图安逸。 今天多学一些知识,明天开发的速度就更快一下。后天你就会变得更好。