C# DataTable转List<Model>通用类
2013-04-26 10:59
447 查看
/// <summary>
/// DataTable转换为List<Model>
/// </summary>
public static class DataTableToListModel<T> where T : new()
{
public static IList<T> ConvertToModel(DataTable dt)
{
//定义集合
IList<T> ts = new List<T>();
T t = new T();
string tempName = "";
//获取此模型的公共属性
PropertyInfo[] propertys = t.GetType().GetProperties();
foreach (DataRow row in dt.Rows)
{
t = new T();
foreach (PropertyInfo pi in propertys)
{
tempName = pi.Name;
//检查DataTable是否包含此列
if (dt.Columns.Contains(tempName))
{
//判断此属性是否有set
if (!pi.CanWrite)
continue;
object value = row[tempName];
if (value != DBNull.Value)
pi.SetValue(t, value, null);
}
}
ts.Add(t);
}
return ts;
}
}
/// DataTable转换为List<Model>
/// </summary>
public static class DataTableToListModel<T> where T : new()
{
public static IList<T> ConvertToModel(DataTable dt)
{
//定义集合
IList<T> ts = new List<T>();
T t = new T();
string tempName = "";
//获取此模型的公共属性
PropertyInfo[] propertys = t.GetType().GetProperties();
foreach (DataRow row in dt.Rows)
{
t = new T();
foreach (PropertyInfo pi in propertys)
{
tempName = pi.Name;
//检查DataTable是否包含此列
if (dt.Columns.Contains(tempName))
{
//判断此属性是否有set
if (!pi.CanWrite)
continue;
object value = row[tempName];
if (value != DBNull.Value)
pi.SetValue(t, value, null);
}
}
ts.Add(t);
}
return ts;
}
}
相关文章推荐
- DataTable转换为List<Model>的通用类
- DataTable转换为List<Model>的通用类
- DataTable转换为List<Model>的通用类
- DataTable转List<Model>通用类
- DataTable转换为List<Model>的通用类
- DataTable转换为List<Model>的通用类
- DataTable转换为List<Model>的通用类(绑定Repeater智能提示)
- DataTable转换为List<Model>的通用类(绑定Repeater智能提示)
- DataTable转换为List<Model>的通用类
- DataTable转换为List<Model>的通用类
- 从DataTable到List<Model>(C#.net)
- DataTable转换为List<Model>的通用类
- DataTable转List<Model>通用类【实体转换辅助类】
- DataTable转List<Model>通用类
- DataTable转换为List<Model>的通用类
- C# 将DataTable装换位List<T> 泛型
- c# Json List<T> DataTable 数据相互转换
- C#中DataTable转化为List<T>解析
- c# Json List<T> DataTable 数据相互转换
- C# DataTable 转换成List<T>