经常使用的一些datatable的操作,包括一些过滤去重的方法
using System; using System.Collections; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Data; namespace TravcomRptTest.Common { public abstract class Dt { #region 按条件过滤DataTable public static DataTable TblFilter(DataTable sourceTable, string condition) { var tempDt = sourceTable.Clone(); var rows = sourceTable.Select(condition); foreach (var dr in rows) { tempDt.ImportRow(dr); } return tempDt; } public static DataTable TblFilter(DataTable sourceTable, string condition, string[] columns) { var tempDt = new DataTable(); foreach (var t in columns) { tempDt.Columns.Add(t, typeof(String)); } var rows = sourceTable.Select(condition); foreach (var dr in rows) { var newDr = tempDt.NewRow(); foreach (var t in columns) { newDr[t.Split(':')[0]] = dr[t.Split(':')[0]].ToString(); } tempDt.Rows.Add(newDr); } return tempDt; } #endregion #region 按条件过滤DataTable,并排序 public static DataTable TblFilter(DataTable sourceTable, string condition, string orderBy) { var tempDt = sourceTable.Clone(); var rows = sourceTable.Select(condition, orderBy); foreach (var dr in rows) { tempDt.ImportRow(dr); } return tempDt; } #endregion #region 筛选不重复的sourceColumn数据,单Column public static DataTable TblDistinct(DataTable sourceTable, string sourceColumn) { try { var result = sourceTable.Clone(); var ht = new Hashtable(); foreach (var dr in sourceTable.Rows.Cast<DataRow>().Where(dr => !ht.ContainsKey(dr[sourceColumn]))) { ht.Add(dr[sourceColumn], null); var newRow = result.NewRow(); newRow.ItemArray = dr.ItemArray; result.Rows.Add(newRow); } return result; } catch (Exception) { return null; } } #endregion #region 筛选不重复的sourceColumn数据,单Column,指定数据类型 public static DataTable TblDistinct(DataTable sourceTable, string sourceColumn, Type columnType) { var result = sourceTable.Clone(); result.TableName = "DT"; result.Columns[sourceColumn].DataType = columnType; try { var ht = new Hashtable(); foreach (var dr in sourceTable.Rows.Cast<DataRow>().Where(dr => !ht.ContainsKey(dr[sourceColumn]))) { ht.Add(dr[sourceColumn], null); var newRow = result.NewRow(); newRow.ItemArray = dr.ItemArray; result.Rows.Add(newRow); } return result; } catch (Exception) { return null; } } #endregion #region 筛选不重复的sourceColumn数据,双Column public static DataTable TblDistinct(DataTable sourceTable, string key1, string key2) { try { var result = sourceTable.Clone(); var ht = new Hashtable(); foreach (var dr in sourceTable.Rows.Cast<DataRow>().Where(dr => !ht.ContainsKey(dr[key1] + dr[key2].ToString()))) { ht.Add(dr[key1] + dr[key2].ToString(), null); var newRow = result.NewRow(); newRow.ItemArray = dr.ItemArray; result.Rows.Add(newRow); } return result; } catch (Exception) { return null; } } #endregion #region 筛选不重复的sourceColumn数据,多Column public static DataTable TblDistinct(DataTable sourceTable, string[] arrKey) { try { var result = sourceTable.Clone(); var ht = new Hashtable(); foreach (DataRow dr in sourceTable.Rows) { var strKey = arrKey.Aggregate(string.Empty, (current, t) => current + dr[t]); if (ht.ContainsKey(strKey)) continue; ht.Add(strKey, null); var newRow = result.NewRow(); newRow.ItemArray = dr.ItemArray; result.Rows.Add(newRow); } return result; } catch (Exception) { return null; } } #endregion #region 获取单一值 public static string GetSingle(string columnName, string condition, DataTable dt) { try { return TblFilter(dt, condition).Rows[0][columnName].ToString(); } catch { return string.Empty; } } //获取单一值,为空返回默认值 public static string GetSingle(string columnName, string condition, DataTable dt, string defaultValue) { string strReturn; try { strReturn = TblFilter(dt, condition).Rows[0][columnName].ToString(); } catch { strReturn = string.Empty; } if (strReturn.Trim() == "") { strReturn = defaultValue; } return strReturn; } #endregion #region 获得一个带结构的DataTable public static DataTable GetNewTbl(string tableName) { var dt = new DataTable(); switch (tableName) { case "ConfigModel": #region dt.Columns.Add("Name", typeof(String)); dt.Columns.Add("id", typeof(String)); dt.Columns.Add("MI01", typeof(String)); #endregion break; } return dt; } #endregion //public static DataTable listToTable(List<Model.Segment> segList) //{ // DataTable dt = new DataTable(); // dt.Columns.Add("psgqueue"); // dt.Columns.Add("airqueue"); // dt.Columns.Add("ticketno"); // for (int i = 0; i < segList.Count; i++) // { // DataRow dr = dt.NewRow(); // dr["psgqueue"] = segList[i].psgqueue; // dr["airqueue"] = segList[i].airQueue; // dr["ticketno"] = segList[i].ticketno; // dt.Rows.Add(dr); // } // return dt; //} } }