http://www.cnblogs.com/envelope/archive/2010/04/20/1716343.html
014 | using System.Reflection; |
015 | using System.Collections; |
016 | using System.Collections.Generic; |
018 | public class IListDataSet |
024 | /// <param name="list">集合</param> |
025 | /// <returns></returns> |
026 | /// 2008-08-01 22:08 HPDV2806 |
027 | public static DataSet ToDataSet(IList p_List) |
029 | DataSet result = new DataSet(); |
030 | DataTable _DataTable = new DataTable(); |
031 | if (p_List.Count > 0) |
033 | PropertyInfo[] propertys = p_List[0].GetType().GetProperties(); |
034 | foreach (PropertyInfo pi in propertys) |
036 | _DataTable.Columns.Add(pi.Name, pi.PropertyType); |
039 | for ( int i = 0; i < p_List.Count; i++) |
041 | ArrayList tempList = new ArrayList(); |
042 | foreach (PropertyInfo pi in propertys) |
044 | object obj = pi.GetValue(p_List[i], null ); |
047 | object [] array = tempList.ToArray(); |
048 | _DataTable.LoadDataRow(array, true ); |
051 | result.Tables.Add(_DataTable); |
058 | /// <typeparam name="T"></typeparam> |
059 | /// <param name="list">泛型集合</param> |
060 | /// <returns></returns> |
061 | /// 2008-08-01 22:43 HPDV2806 |
062 | public static DataSet ToDataSet<T>(IList<T> list) |
064 | return ToDataSet<T>(list, null ); |
071 | /// <typeparam name="T"></typeparam> |
072 | /// <param name="p_List">泛型集合</param> |
073 | /// <param name="p_PropertyName">待转换属性名数组</param> |
074 | /// <returns></returns> |
075 | /// 2008-08-01 22:44 HPDV2806 |
076 | public static DataSet ToDataSet<T>(IList<T> p_List, params string [] p_PropertyName) |
078 | List< string > propertyNameList = new List< string >(); |
079 | if (p_PropertyName != null ) |
080 | propertyNameList.AddRange(p_PropertyName); |
082 | DataSet result = new DataSet(); |
083 | DataTable _DataTable = new DataTable(); |
084 | if (p_List.Count > 0) |
086 | PropertyInfo[] propertys = p_List[0].GetType().GetProperties(); |
087 | foreach (PropertyInfo pi in propertys) |
089 | if (propertyNameList.Count == 0) |
092 | _DataTable.Columns.Add(pi.Name, pi.PropertyType); |
096 | if (propertyNameList.Contains(pi.Name)) |
097 | _DataTable.Columns.Add(pi.Name, pi.PropertyType); |
101 | for ( int i = 0; i < p_List.Count; i++) |
103 | ArrayList tempList = new ArrayList(); |
104 | foreach (PropertyInfo pi in propertys) |
106 | if (propertyNameList.Count == 0) |
108 | object obj = pi.GetValue(p_List[i], null ); |
113 | if (propertyNameList.Contains(pi.Name)) |
115 | object obj = pi.GetValue(p_List[i], null ); |
120 | object [] array = tempList.ToArray(); |
121 | _DataTable.LoadDataRow(array, true ); |
124 | result.Tables.Add(_DataTable); |
131 | /// <typeparam name="T"></typeparam> |
132 | /// <param name="p_DataSet">DataSet</param> |
133 | /// <param name="p_TableIndex">待转换数据表索引</param> |
134 | /// <returns></returns> |
135 | /// 2008-08-01 22:46 HPDV2806 |
136 | public static IList<T> DataSetToIList<T>(DataSet p_DataSet, int p_TableIndex) |
138 | if (p_DataSet == null || p_DataSet.Tables.Count < 0) |
140 | if (p_TableIndex > p_DataSet.Tables.Count - 1) |
142 | if (p_TableIndex < 0) |
145 | DataTable p_Data = p_DataSet.Tables[p_TableIndex]; |
147 | IList<T> result = new List<T>(); |
148 | for ( int j = 0; j < p_Data.Rows.Count; j++) |
150 | T _t = (T)Activator.CreateInstance( typeof (T)); |
151 | PropertyInfo[] propertys = _t.GetType().GetProperties(); |
152 | foreach (PropertyInfo pi in propertys) |
154 | for ( int i = 0; i < p_Data.Columns.Count; i++) |
157 | if (pi.Name.Equals(p_Data.Columns[i].ColumnName)) |
160 | if (p_Data.Rows[j][i] != DBNull.Value) |
161 | pi.SetValue(_t, p_Data.Rows[j][i], null ); |
163 | pi.SetValue(_t, null , null ); |
176 | /// <typeparam name="T"></typeparam> |
177 | /// <param name="p_DataSet">DataSet</param> |
178 | /// <param name="p_TableName">待转换数据表名称</param> |
179 | /// <returns></returns> |
180 | /// 2008-08-01 22:47 HPDV2806 |
181 | public static IList<T> DataSetToIList<T>(DataSet p_DataSet, string p_TableName) |
184 | if (p_DataSet == null || p_DataSet.Tables.Count < 0) |
186 | if ( string .IsNullOrEmpty(p_TableName)) |
188 | for ( int i = 0; i < p_DataSet.Tables.Count; i++) |
191 | if (p_DataSet.Tables[i].TableName.Equals(p_TableName)) |
197 | return DataSetToIList<T>(p_DataSet, _TableIndex); |
转载于:https://www.cnblogs.com/infim/archive/2011/03/31/2001380.html