在线求等大神解决Dataset转换成List问题 winfrom 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 public static List<T> EntityList<T>(DataTable dt) { if (dt == null || dt.Rows.Count == 0) { return null; } List<T> list = new List<T>(); T entity = default(T); foreach (DataRow dr in dt.Rows) { entity = Activator.CreateInstance<T>(); PropertyInfo[] pis = entity.GetType().GetProperties(); foreach (PropertyInfo pi in pis) { if (dt.Columns.Contains(pi.Name)) { if (!pi.CanWrite) { continue; } if (dr[pi.Name] != DBNull.Value) { Type t = pi.PropertyType; if (t.FullName == "System.Guid") { pi.SetValue(entity, Guid.Parse(dr[pi.Name].ToString()), null); } else { pi.SetValue(entity, dr[pi.Name], null); } } } } list.Add(entity); } return list; } dictionary效率问题 正则表达式-获取div,h3,table>tr>td标签中的字符 IF 10位数时 OK...等等 datatable 绑定datagridview 问题 高分求救:DataTable转换成IList<T> WebBrowser的HtmlDocument如何创建Body? OleDbParameter类怎么使用?用来干什么? c#调用C#写的dll的一个问题? 进程条和数据库连接 如何找出指定字符串中的特定字符串? c# 类的定义中把自己实例化是什么意思. 获取本机ip显示在listbox中,需要显示ipv4
public static List<T> EntityList<T>(DataTable dt)
{
if (dt == null || dt.Rows.Count == 0)
{
return null;
}
List<T> list = new List<T>();
T entity = default(T);
foreach (DataRow dr in dt.Rows)
{
entity = Activator.CreateInstance<T>();
PropertyInfo[] pis = entity.GetType().GetProperties();
foreach (PropertyInfo pi in pis)
{
if (dt.Columns.Contains(pi.Name))
{
if (!pi.CanWrite)
{
continue;
}
if (dr[pi.Name] != DBNull.Value)
{
Type t = pi.PropertyType;
if (t.FullName == "System.Guid")
{
pi.SetValue(entity, Guid.Parse(dr[pi.Name].ToString()), null);
}
else
{
pi.SetValue(entity, dr[pi.Name], null);
}
}
}
}
list.Add(entity);
}
return list;
}