发布:2022/12/20 16:37:59作者:管理员 来源:本站 浏览次数:540
一、 DataTable转换到List<T>
/// <summary>
/// TableToList
/// </summary>
public class TableListConverter<T> where T : class, new()
{
public static IList<T> TableToList(DataTable dt)
{
IList<T> ts = new List<T>();// 定义集合
Type type = typeof(T);// 获得此模型的类型
string tempName = "";
foreach (DataRow dr in dt.Rows)
{
T t = new T();
// 获得此模型的公共属性
PropertyInfo[] propertys = t.GetType().GetProperties();
foreach (PropertyInfo pi in propertys)
{
tempName = pi.Name;
if (dt.Columns.Contains(tempName))// 检查DataTable是否包含此列
{
if (!pi.CanWrite) continue;// 判断此属性是否有Setter
object value = dr[tempName];
if (value != DBNull.Value)
pi.SetValue(t, value, null);
}
}
ts.Add(t);
}
return ts;
}
}
应用:
// 获得查询结果
DataTable dt = DbHelper.ExecuteDataTable("...");
// 把DataTable转换为IList<UserInfo>
IList<UserInfo> users = TableListConverter<UserInfo>.TableToList(dt);
二、 List<T>转换到DataTable
/// <summary>
/// ListToTable
/// </summary>
public class TableListConverter
{
public static DataTable ListToTable<T>(IList<T> list) where T : class, new()
{
if (list == null) return null;
Type type = typeof(T);
DataTable dt = new DataTable();
PropertyInfo[] properties = Array.FindAll(type.GetProperties(), p => p.CanRead);//判断此属性是否有Getter
Array.ForEach(properties, prop => { dt.Columns.Add(prop.Name, prop.PropertyType); });//添加到列
foreach (T t in list)
{
DataRow row = dt.NewRow();
Array.ForEach(properties, prop =>
{
row[prop.Name] = prop.GetValue(t, null);
});//添加到行
dt.Rows.Add(row);
}
return dt;
}
}
应用:
//IList<UserInfo> users
DataTable dt =TableListConverter.ListToTable(users)
1、List转DataTable
/// <summary>
/// list to datatable
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="collection"></param>
/// <returns></returns>
public DataTable ListToDt<T>(IEnumerable<T> collection)
{
var props = typeof(T).GetProperties();
var dt = new DataTable();
dt.Columns.AddRange(props.Select(p => new
DataColumn(p.Name, p.PropertyType)).ToArray());
if (collection.Count() > 0)
{
for (int i = 0; i < collection.Count(); i++)
{
ArrayList tempList = new ArrayList();
foreach (PropertyInfo pi in props)
{
object obj = pi.GetValue(collection.ElementAt(i), null);
tempList.Add(obj);
}
object[] array = tempList.ToArray();
dt.LoadDataRow(array, true);
}
}
return dt;
}
2、DataTable转List
public class DtToList<T> where T : new()
{
/// <summary>
/// datatable to list
/// </summary>
/// <param name="dt"></param>
/// <returns></returns>
public static List<T> ConvertToModel(DataTable dt)
{
List<T> ts = new List<T>();// 定义集合
Type type = typeof(T); // 获得此模型的类型
string tempName = "";
foreach (DataRow dr in dt.Rows)
{
T t = new T();
PropertyInfo[] propertys = t.GetType().GetProperties();// 获得此模型的公共属性
foreach (PropertyInfo pi in propertys)
{
tempName = pi.Name;
if (dt.Columns.Contains(tempName))
{
if (!pi.CanWrite) continue;
object value = dr[tempName];
if (value != DBNull.Value)
pi.SetValue(t, value, null);
}
}
ts.Add(t);
}
return ts;
}
}
© Copyright 2014 - 2024 柏港建站平台 ejk5.com. 渝ICP备16000791号-4