/// <summary>
/// DataTable转换为List
/// </summary>
public static IList<T> DataTable2List<T>(DataTable dt)
{
IList<T> list = new List<T>();
Type type = typeof(T);
string piName = "";
foreach (DataRow dr in dt.Rows)
{
T t = (T)Activator.CreateInstance(typeof(T));
PropertyInfo[] propertys = t.GetType().GetProperties();
foreach (PropertyInfo pi in propertys)
{
piName = pi.Name;
if (dt.Columns.Contains(piName))
{
if (!pi.CanWrite)
continue;
object value = dr[piName];
if (value != DBNull.Value)
pi.SetValue(t, value, null);
else
pi.SetValue(t, null, null);
}
}
list.Add(t);
}
return list;
}
/// DataTable转换为List
/// </summary>
public static IList<T> DataTable2List<T>(DataTable dt)
{
IList<T> list = new List<T>();
Type type = typeof(T);
string piName = "";
foreach (DataRow dr in dt.Rows)
{
T t = (T)Activator.CreateInstance(typeof(T));
PropertyInfo[] propertys = t.GetType().GetProperties();
foreach (PropertyInfo pi in propertys)
{
piName = pi.Name;
if (dt.Columns.Contains(piName))
{
if (!pi.CanWrite)
continue;
object value = dr[piName];
if (value != DBNull.Value)
pi.SetValue(t, value, null);
else
pi.SetValue(t, null, null);
}
}
list.Add(t);
}
return list;
}