我有一个强类型的DataTable类型MyType
,我想转换它List
.
我怎样才能做到这一点 ?
谢谢.
以下是一行:
dataTable.Rows.OfType() .Select(dr => dr.Field (columnName)).ToList();
[ 编辑:System.Data.DataSetExtensions
如果不能编译,请添加对项目的引用]
ListlistName = dataTableName.AsEnumerable().Select(m => new MyType() { ID = m.Field ("ID"), Description = m.Field ("Description"), Balance = m.Field ("Balance"), }).ToList()
DataTable有Linq扩展方法.
添加参考: System.Data.DataSetExtensions.dll
然后包括命名空间: using System.Data.DataSetExtensions
最后,您可以在DataSet和DataTables上使用Linq扩展:
var matches = myDataSet.Tables.First().Where(dr=>dr.Field("id") == 1);
在.Net 2.0上,您仍然可以添加泛型方法:
public static ListConvertRowsToList ( DataTable input, Convert conversion) { List retval = new List () foreach(DataRow dr in input.Rows) retval.Add( conversion(dr) ); return retval; }
数据表到列表
#region "getobject filled object with property reconized" public ListConvertTo (DataTable datatable) where T : new() { List Temp = new List (); try { List columnsNames = new List (); foreach (DataColumn DataColumn in datatable.Columns) columnsNames.Add(DataColumn.ColumnName); Temp = datatable.AsEnumerable().ToList().ConvertAll (row => getObject (row, columnsNames)); return Temp; } catch { return Temp; } } public T getObject (DataRow row, List columnsName) where T : new() { T obj = new T(); try { string columnname = ""; string value = ""; PropertyInfo[] Properties; Properties = typeof(T).GetProperties(); foreach (PropertyInfo objProperty in Properties) { columnname = columnsName.Find(name => name.ToLower() == objProperty.Name.ToLower()); if (!string.IsNullOrEmpty(columnname)) { value = row[columnname].ToString(); if (!string.IsNullOrEmpty(value)) { if (Nullable.GetUnderlyingType(objProperty.PropertyType) != null) { value = row[columnname].ToString().Replace("$", "").Replace(",", ""); objProperty.SetValue(obj, Convert.ChangeType(value, Type.GetType(Nullable.GetUnderlyingType(objProperty.PropertyType).ToString())), null); } else { value = row[columnname].ToString().Replace("%", ""); objProperty.SetValue(obj, Convert.ChangeType(value, Type.GetType(objProperty.PropertyType.ToString())), null); } } } } return obj; } catch { return obj; } } #endregion
IEnumerable集合到数据表
#region "New DataTable" public DataTable ToDataTable(IEnumerable collection) { DataTable newDataTable = new DataTable(); Type impliedType = typeof(T); PropertyInfo[] _propInfo = impliedType.GetProperties(); foreach (PropertyInfo pi in _propInfo) newDataTable.Columns.Add(pi.Name, pi.PropertyType); foreach (T item in collection) { DataRow newDataRow = newDataTable.NewRow(); newDataRow.BeginEdit(); foreach (PropertyInfo pi in _propInfo) newDataRow[pi.Name] = pi.GetValue(item, null); newDataRow.EndEdit(); newDataTable.Rows.Add(newDataRow); } return newDataTable; }
下面发布并使用反射的ConvertToList方法对我来说非常有效.谢谢.
我做了一些修改,使其适用于T属性类型的转换.
public ListConvertToList (DataTable dt) { var columnNames = dt.Columns.Cast () .Select(c => c.ColumnName) .ToList(); var properties = typeof(T).GetProperties(); return dt.AsEnumerable().Select(row => { var objT = Activator.CreateInstance (); foreach (var pro in properties) { if (columnNames.Contains(pro.Name)) { PropertyInfo pI = objT.GetType().GetProperty(pro.Name); pro.SetValue(objT, row[pro.Name] == DBNull.Value ? null : Convert.ChangeType(row[pro.Name], pI.PropertyType)); } } return objT; }).ToList(); }
希望能帮助到你.问候.