泛型集合与DataSet相互转换

时间:2022-10-17 22:23:18

一、泛型转DataSet

/// <summary>
/// 泛型集合转换DataSet
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="list">泛型集合</param>
/// <returns></returns>
///
public static DataSet ToDataSet<T>(List<T> list)
{
return ToDataSet1<T>(list, null);
}
/// <summary>
/// 泛型集合转换DataSet,并指定需要转换的字段
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="p_List">泛型集合</param>
/// <param name="p_PropertyName">待转换属性名数组</param>
/// <returns></returns>
public static DataSet ToDataSet1<T>(List<T> p_List, params string[] p_PropertyName)
{
List<string> propertyNameList = new List<string>();
if (p_PropertyName != null)
propertyNameList.AddRange(p_PropertyName); DataSet result = new DataSet();
DataTable _DataTable = new DataTable();
if (p_List.Count > )
{
Type type = typeof(T);
PropertyInfo[] propertys = type.GetProperties();
foreach (PropertyInfo pi in propertys)
{
if (propertyNameList.Count == )
{
// 没有指定属性的情况下全部属性都要转换
_DataTable.Columns.Add(pi.Name, pi.PropertyType);
}
else
{
if (propertyNameList.Contains(pi.Name))
_DataTable.Columns.Add(pi.Name, pi.PropertyType);
}
} for (int i = ; i < p_List.Count; i++)
{
ArrayList tempList = new ArrayList();
foreach (PropertyInfo pi in propertys)
{
if (propertyNameList.Count == )
{
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;
} //调用
// List<实体类> List = new List<实体类>();
//DataSet ds = ToDataSet(List);

二、DataSet转泛型

第一种通过索引

/// <summary>
/// DataSet装换为泛型集合
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="p_DataSet">DataSet</param>
/// <param name="p_TableIndex">待转换数据表索引</param>
/// <returns></returns>
public static List<T> DataSetToList<T>(DataSet p_DataSet, int p_TableIndex)
{
if (p_DataSet == null || p_DataSet.Tables.Count < )
return null;
if (p_TableIndex > p_DataSet.Tables.Count - )
return null;
if (p_TableIndex < )
p_TableIndex = ; DataTable p_Data = p_DataSet.Tables[p_TableIndex];
// 返回值初始化
List<T> result = new List<T>();
for (int j = ; j < p_Data.Rows.Count; j++)
{
T _t = (T)Activator.CreateInstance(typeof(T));
PropertyInfo[] propertys = _t.GetType().GetProperties();
foreach (PropertyInfo pi in propertys)
{
for (int i = ; i < p_Data.Columns.Count; i++)
{
// 属性与字段名称一致的进行赋值
if (pi.Name.Equals(p_Data.Columns[i].ColumnName.ToUpper()))
{
// 数据库NULL值单独处理
if (p_Data.Rows[j][i] != DBNull.Value)
{
if (p_Data.Rows[j][i].GetType().Name == "Double")
{
pi.SetValue(_t, Convert.ToDecimal(p_Data.Rows[j][i]), null);
}
else
{
pi.SetValue(_t, p_Data.Rows[j][i], null);
}
}
else
pi.SetValue(_t, null, null);
break;
}
}
}
result.Add(_t);
}
return result;
}
//调用
DataSet ds = new DataSet();
List<实体类> list = new List<实体类>();
dddss.Tables.Add(dt);
//组合条件筛选完成之后转换为实体集合
list = DataSetToList<实体类>(ds, 索引);

第二种通过数据表名称

/// <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 List<T> DataSetToList<T>(DataSet p_DataSet, string p_TableName)
{
int _TableIndex = ;
if (p_DataSet == null || p_DataSet.Tables.Count < )
return null;
if (string.IsNullOrEmpty(p_TableName))
return null;
for (int i = ; i < p_DataSet.Tables.Count; i++)
{
// 获取Table名称在Tables集合中的索引值
if (p_DataSet.Tables[i].TableName.Equals(p_TableName))
{
_TableIndex = i;
break;
}
}
return DataSetToList<T>(p_DataSet, _TableIndex);
}
/// <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 List<T> DataSetToList<T>(DataSet p_DataSet, int p_TableIndex)
{
if (p_DataSet == null || p_DataSet.Tables.Count < )
return null;
if (p_TableIndex > p_DataSet.Tables.Count - )
return null;
if (p_TableIndex < )
p_TableIndex = ; DataTable p_Data = p_DataSet.Tables[p_TableIndex];
// 返回值初始化
List<T> result = new List<T>();
for (int j = ; j < p_Data.Rows.Count; j++)
{
T _t = (T)Activator.CreateInstance(typeof(T));
PropertyInfo[] propertys = _t.GetType().GetProperties();
foreach (PropertyInfo pi in propertys)
{
for (int i = ; i < p_Data.Columns.Count; i++)
{
// 属性与字段名称一致的进行赋值
if (pi.Name.Equals(p_Data.Columns[i].ColumnName.ToUpper()))
{
// 数据库NULL值单独处理
if (p_Data.Rows[j][i] != DBNull.Value)
{
if (p_Data.Rows[j][i].GetType().Name == "Double")
{
pi.SetValue(_t, Convert.ToDecimal(p_Data.Rows[j][i]), null);
}
else
{
pi.SetValue(_t, p_Data.Rows[j][i], null);
}
}
else
pi.SetValue(_t, null, null);
break;
}
}
}
result.Add(_t);
}
return result;
}
//调用
DataSet ds = new DataSet();
List<实体类> list = new List<实体类>();
dddss.Tables.Add(dt);
//组合条件筛选完成之后转换为实体集合
list = DataSetToList<实体类>(ds, 表名);