using System; using System.Collections; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Linq; using System.Reflection; using System.Text; namespace EasyBL { public static class ComTool { /// /// Converts DataTable To List /// /// /// /// public static List ToList_1(this DataTable dataTable) where TSource : new() { var dataList = new List(); const BindingFlags flags = BindingFlags.Public | BindingFlags.Instance | BindingFlags.NonPublic; var objFieldNames = (from PropertyInfo aProp in typeof(TSource).GetProperties(flags) select new { aProp.Name, Type = Nullable.GetUnderlyingType(aProp.PropertyType) ?? aProp.PropertyType }).ToList(); var dataTblFieldNames = (from DataColumn aHeader in dataTable.Columns select new { Name = aHeader.ColumnName, Type = aHeader.DataType }).ToList(); var commonFields = objFieldNames.Intersect(dataTblFieldNames).ToList(); foreach (DataRow dataRow in dataTable.AsEnumerable().ToList()) { var aTSource = new TSource(); foreach (var aField in commonFields) { var propertyInfos = aTSource.GetType().GetProperty(aField.Name); var value = (dataRow[aField.Name] == DBNull.Value) ? null : dataRow[aField.Name]; //if database field is nullable propertyInfos.SetValue(aTSource, value, null); } dataList.Add(aTSource); } return dataList; } /// /// 將自動轉table。 /// 通常會搭配 /// /// /// /// /// public static DataTable ListToDataTable(this IList data, string tableName = "tb") { var table = new DataTable(tableName); //special handling for value types and string if (typeof(T).IsValueType || typeof(T).Equals(typeof(string))) { var dc = new DataColumn("Value"); table.Columns.Add(dc); foreach (T item in data) { var dr = table.NewRow(); dr[0] = item; table.Rows.Add(dr); } } else { var properties = TypeDescriptor.GetProperties(typeof(T)); foreach (PropertyDescriptor prop in properties) { table.Columns.Add(prop.Name, Nullable.GetUnderlyingType(prop.PropertyType) ?? prop.PropertyType); } foreach (T item in data) { var row = table.NewRow(); foreach (PropertyDescriptor prop in properties) { try { row[prop.Name] = prop.GetValue(item) ?? DBNull.Value; } catch (Exception) { row[prop.Name] = DBNull.Value; } } table.Rows.Add(row); } } return table; } /// /// todo: describe list parameter on ToDataTabletodo: describe list parameter on ToDataTable 转化一个DataTable /// /// todo: describe list parameter on ToDataTable /// /// /// public static DataTable ToDataTable(this IEnumerable list) { //创建属性的集合 var pList = new List(); //获得反射的入口 var type = typeof(T); var dt = new DataTable(); //把所有的public属性加入到集合 并添加DataTable的列 Array.ForEach(type.GetProperties(), p => { pList.Add(p); dt.Columns.Add(p.Name, p.PropertyType); }); foreach (var item in list) { //创建一个DataRow实例 var row = dt.NewRow(); //给row 赋值 pList.ForEach(p => row[p.Name] = p.GetValue(item, null)); //加入到DataTable dt.Rows.Add(row); } return dt; } /// /// DataTable 转换为List 集合 /// /// 类型 /// DataTable /// public static List ToList(this DataTable dt) where T : class, new() { //创建一个属性的列表 var prlist = new List(); //获取TResult的类型实例 反射的入口 var t = typeof(T); //获得TResult 的所有的Public 属性 并找出TResult属性和DataTable的列名称相同的属性(PropertyInfo) 并加入到属性列表 Array.ForEach(t.GetProperties(), p => { if (dt.Columns.IndexOf(p.Name) != -1) prlist.Add(p); }); //创建返回的集合 var oblist = new List(); foreach (DataRow row in dt.Rows) { //创建TResult的实例 var ob = new T(); //找到对应的数据 并赋值 prlist.ForEach(p => { if (row[p.Name] != DBNull.Value) p.SetValue(ob, row[p.Name], null); }); //放入到返回的集合中. oblist.Add(ob); } return oblist; } /// /// 将集合类转换成DataTable /// /// 集合 /// public static DataTable ToDataTableTow(IList list) { var result = new DataTable(); if (list.Count > 0) { var propertys = list[0].GetType().GetProperties(); foreach (PropertyInfo pi in propertys) { result.Columns.Add(pi.Name, pi.PropertyType); } for (int i = 0; i < list.Count; i++) { var tempList = new ArrayList(); foreach (PropertyInfo pi in propertys) { var obj = pi.GetValue(list[i], null); tempList.Add(obj); } var array = tempList.ToArray(); result.LoadDataRow(array, true); } } return result; } /// /// 将集合类转换成DataTable /// /// 集合 /// public static DataTable DicToDataTable(List> list) { var result = new DataTable("tb"); if (list.Count > 0) { var dic = list[0]; foreach (string key in dic.Keys) { result.Columns.Add(key); } foreach (Dictionary info in list) { var tempList = new ArrayList(); foreach (string key in info.Keys) { tempList.Add(info[key]); } var array = tempList.ToArray(); result.LoadDataRow(array, true); } } return result; } /// /// 将泛型集合类转换成DataTable todo: describe list parameter on /// ToDataTabletodo: describe list parameter on ToDataTable /// /// 集合项类型 /// 集合 /// 数据集(表) public static DataTable ToDataTable(IList list) { return ToDataTable(list, null); } /// /// 将泛型集合类转换成DataTable /// /// 集合项类型 /// 集合 /// 需要返回的列的列名 /// 数据集(表) public static DataTable ToDataTable(IList list, params string[] propertyName) { var propertyNameList = new List(); if (propertyName != null) propertyNameList.AddRange(propertyName); var result = new DataTable(); if (list.Count > 0) { var propertys = list[0].GetType().GetProperties(); foreach (PropertyInfo pi in propertys) { if (propertyNameList.Count == 0) { result.Columns.Add(pi.Name, pi.PropertyType); } else { if (propertyNameList.Contains(pi.Name)) result.Columns.Add(pi.Name, pi.PropertyType); } } for (int i = 0; i < list.Count; i++) { var tempList = new ArrayList(); foreach (PropertyInfo pi in propertys) { if (propertyNameList.Count == 0) { var obj = pi.GetValue(list[i], null); tempList.Add(obj); } else { if (propertyNameList.Contains(pi.Name)) { var obj = pi.GetValue(list[i], null); tempList.Add(obj); } } } var array = tempList.ToArray(); result.LoadDataRow(array, true); } } return result; } public static string PadLeftEx(string str, int totalByteCount, char c) { var coding = Encoding.GetEncoding("gb2312"); var dcount = 0; foreach (char ch in str.ToCharArray()) { if (coding.GetByteCount(ch.ToString()) == 2) dcount++; } var w = str.PadRight(totalByteCount - dcount, c); return w; } public static string PadRightEx(string str, int totalByteCount, char c) { var coding = Encoding.GetEncoding("gb2312"); var dcount = 0; foreach (char ch in str.ToCharArray()) { if (coding.GetByteCount(ch.ToString()) == 2) dcount++; } var w = str.PadRight(totalByteCount - dcount, c); return w; } } }