List<T> DataSet 转换
2013-01-30 15:43
405 查看
1.List-->DataSet
2.List<T>-->DataSet
3.DataSet-->List<T>
2.List<T>-->DataSet
3.DataSet-->List<T>
using System; using System.Collections; using System.Collections.Generic; using System.Linq; using System.Data; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; using System.Text; using System.Reflection; public partial class Default2 : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { } /// <summary> /// 集合装换DataSet /// </summary> /// <param name="list">集合</param> /// <returns></returns> /// 2008-08-01 22:08 HPDV2806 public static DataSet ToDataSet(IList p_List) { DataSet result = new DataSet(); DataTable _DataTable = new DataTable(); if (p_List.Count > 0) { PropertyInfo[] propertys = p_List[0].GetType().GetProperties(); foreach (PropertyInfo pi in propertys) { _DataTable.Columns.Add(pi.Name, pi.PropertyType);//columnName Type } for (int i = 0; i < p_List.Count; i++) { ArrayList tempList = new ArrayList(); //一行数据的数组 foreach (PropertyInfo pi in propertys) { object obj = pi.GetValue(p_List[i], null);//p_List[i]是集合中的某一个对象,GetValue 返回其属性值 tempList.Add(obj); } object[] array = tempList.ToArray(); _DataTable.LoadDataRow(array, true); //LoadDataRow 查找和更新特定行。 如果找不到任何匹配行,则使用给定值创建新行 } } result.Tables.Add(_DataTable); return result; } /// <summary> /// 泛型集合转换DataSet /// </summary> /// <typeparam name="T"></typeparam> /// <param name="list">泛型集合</param> /// <returns></returns> /// 2008-08-01 22:43 HPDV2806 public static DataSet ToDataSet<T>(IList<T> list) { return ToDataSet<T>(list, null); } /// <summary> /// 泛型集合转换DataSet /// </summary> /// <typeparam name="T"></typeparam> /// <param name="p_List">泛型集合</param> /// <param name="p_PropertyName">待转换属性名数组(null时,表中字段全部转换,有数组时,只转换数组中和表字段都有的字段)</param> /// <returns></returns> /// 2008-08-01 22:44 HPDV2806 public static DataSet ToDataSet<T>(IList<T> p_List, params string[] p_PropertyName) { List<string> propertyNameList = new List<string>(); if (p_PropertyName != null) propertyNameList.AddRange(p_PropertyName); //AddRange 向……末尾,添加数组 DataSet result = new DataSet(); DataTable _DataTable = new DataTable(); if (p_List.Count > 0) { PropertyInfo[] propertys = p_List[0].GetType().GetProperties(); foreach (PropertyInfo pi in propertys) { if (propertyNameList.Count == 0) { // 没有指定属性的情况下全部属性都要转换 _DataTable.Columns.Add(pi.Name, pi.PropertyType); } else { if (propertyNameList.Contains(pi.Name)) _DataTable.Columns.Add(pi.Name, pi.PropertyType); } } for (int i = 0; i < p_List.Count; i++) { ArrayList tempList = new ArrayList(); foreach (PropertyInfo pi in propertys) { if (propertyNameList.Count == 0) { object obj = pi.GetValue(p_List[i], null); tempList.Add(obj); } else { if (propertyNameList.Contains(pi.Name)) { object obj = pi.GetValue(p_List[i], null); tempList.Add(obj); } } } object[] array = tempList.ToArray(); _DataTable.LoadDataRow(array, true); } } result.Tables.Add(_DataTable); return result; } /// <summary> /// DataSet装换为泛型集合 /// </summary> /// <typeparam name="T"></typeparam> /// <param name="p_DataSet">DataSet</param> /// <param name="p_TableIndex">待转换数据表索引</param> /// <returns></returns> /// 2008-08-01 22:46 HPDV2806 public static IList<T> DataSetToIList<T>(DataSet p_DataSet, int p_TableIndex) { if (p_DataSet == null || p_DataSet.Tables.Count < 0) return null; if (p_TableIndex > p_DataSet.Tables.Count - 1) return null; if (p_TableIndex < 0) p_TableIndex = 0; DataTable p_Data = p_DataSet.Tables[p_TableIndex]; // 返回值初始化 IList<T> result = new List<T>(); for (int i = 0; i < p_Data.Rows.Count; i++) { T _t = (T)Activator.CreateInstance(typeof(T));//Activator.CreateInstrance C#在类工厂中动态创建类的实例 PropertyInfo[] propertys = _t.GetType().GetProperties(); foreach (PropertyInfo pi in propertys) { for (int j = 0; j < p_Data.Columns.Count; j++) { // 属性与字段名称一致的进行赋值 if (pi.Name.Equals(p_Data.Columns[j].ColumnName)) { // 数据库NULL值单独处理 if (p_Data.Rows[i][j] != DBNull.Value) pi.SetValue(_t, p_Data.Rows[i][j], null); //SetValue 将给定对象的属性值设置为给定值。 else pi.SetValue(_t, null, null); break; } } } result.Add(_t); } return result; } /// <summary> /// DataSet装换为泛型集合 /// </summary> /// <typeparam name="T"></typeparam> /// <param name="p_DataSet">DataSet</param> /// <param name="p_TableName">待转换数据表名称</param> /// <returns></returns> /// 2008-08-01 22:47 HPDV2806 public static IList<T> DataSetToIList<T>(DataSet p_DataSet, string p_TableName) { int _TableIndex = 0; if (p_DataSet == null || p_DataSet.Tables.Count < 0) return null; if (string.IsNullOrEmpty(p_TableName)) return null; for (int i = 0; i < p_DataSet.Tables.Count; i++) { // 获取Table名称在Tables集合中的索引值 if (p_DataSet.Tables[i].TableName.Equals(p_TableName)) { _TableIndex = i; break; } } return DataSetToIList<T>(p_DataSet, _TableIndex); } }
相关文章推荐
- c#将List<T>转换成DataSet
- IList<T> 转换成 DataSet
- DataSet和List<T> 泛型之间互相转换
- DateTable与List<T>相互转换 及JSON与DataTable(DataSet)相互转化
- DataSet和List<T> 泛型之间互相转换
- Ilist<T> 转换成 DataSet
- MVC4使用微软企业库 dataset foreach遍历 转换成list<T>
- DataSet和List<T> 泛型之间互相转换 (转载, 作者写的很好)
- IList<T> 转换成 DataSet
- NHibernate实现IList<T> 转换成DataSet
- C#读取数据库返回泛型集合 把DataSet类型转换为List<T>泛型集合
- DataSet和List<T> 泛型之间互相转换
- IList<T> 转换成 DataSet
- 将 IList<T> 或 IList 转换为Dataset或DataTable
- List<> 转换为Dataset的C#代码实现 解决Nullable问题
- C#读取数据库返回泛型集合 把DataSet类型转换为List<T>泛型集合
- DataSet和List<T> 泛型之间互相转换 (转载, 作者写的很好)
- (转自YYW'S BLOG)IList<T> 转换成 DataSet
- Ilist<T> 转换成 DataSet
- C#读取数据库返回泛型集合 把DataSet类型转换为List<T>泛型集合