求好用的dataTable或者DataReader转List<T>程序,
1.dataTable中的列不固定,可能3列也可能10列
2.不只是要方法,还需要如何调用,详细点,本人刚接触List
3.主要是用List排序
4.已经发过贴子了,但都给是方法,不会用,如下面的。public static IList<T> ReaderToList<T>(this IDataReader dr)
  {
  using (dr)
  {
  List<T> list = new List<T>();
  Type modelType = typeof(T);
  int count = dr.FieldCount;
  while (dr.Read())
  {
  T model = Activator.CreateInstance<T>();//错误没有为该对象定义无参数的构造函数。  for (int i = 0; i < count; i++)
  {
  if (!IsNullOrDBNull(dr[i]))
  {//GetPropertyName
  PropertyInfo pi = modelType.GetProperty(dr.GetName(i), BindingFlags.GetProperty | BindingFlags.Public | BindingFlags.Instance | BindingFlags.IgnoreCase);
  if (pi != null)
  {
  pi.SetValue(model, HackType(dr[i], pi.PropertyType), null);
  // pi.SetValue(model, Convert.ChangeType(dr[i], pi.PropertyType), null);
  }
  }
  }
  list.Add(model);
  }
  return list;
  }
  }//T model = Activator.CreateInstance<T>();//错误没有为该对象定义无参数的构造函数。但运行出错了。OleDbDataReader myReader有值,我是用的调用方式
 IList<string> aa = ReaderToList<string>(myReader);
初学,感谢呀!!

解决方案 »

  1.   

    public IList <T> GetList <T>(DataTable table)  
      {  
      IList <T> list = new List <T>();  
      T t = default(T);  
      PropertyInfo[] propertypes = null;  
      string tempName = string.Empty;  
      foreach (DataRow row in table.Rows)  
      {  
      t = Activator.CreateInstance <T>();  
      propertypes = t.GetType().GetProperties();  
      foreach (PropertyInfo pro in propertypes)  
      {  
      tempName = pro.Name;  
      if (table.Columns.Contains(tempName))  
      {  
      object value = row[tempName];  
      pro.SetValue(t, value, null);  
      }  
      }  
      list.Add(t);  
      }  
      return list;  
      }  
      

  2.   

     t = Activator.CreateInstance<T>();错误没有为该对象定义无参数的构造函数。
    如何调用呀?我是这么用的,是不是不对呀!感谢!!
    IList<string> aa = GetList<string>(dt);
      

  3.   

    IList<string> aa = GetList<string>(dt);你有没有将IList的命名空间导入呢?还有你的datatable有值吗?调试一下!
      

  4.   

    楼主。。你引用程序集没有。是net 3.5或者以上吗?
    上面那个程序我一直在用。基本没问题。你可以自行修改。
    这里用的是扩展方法
    是Idatareader.ReaderToList<yourclass>();
    这样用的
      

  5.   

    IList(T)命名空间 System.Collections.Generic 已经导入
    datatable dt有值
    用的是net 3.5
    调试后出错
    t = Activator.CreateInstance<T>();错误没有为该对象定义无参数的构造函数。
      

  6.   

    IList<T> aa = GetList<T>(dt);
    中的T是数据类型行不?一定得是类吗?是不是我这样用的不对呀!
    IList<string> aa = GetList<string>(dt);
    string 是字符串类型
      

  7.   

    public IList <T> GetList <T>(DataTable table)   
      {   
      IList <T> list = new List <T>();   
      T t = default(T);   
      PropertyInfo[] propertypes = null;   
      string tempName = string.Empty;   
      foreach (DataRow row in table.Rows)   
      {   
      t = Activator.CreateInstance <T>();   
      propertypes = t.GetType().GetProperties();   
      foreach (PropertyInfo pro in propertypes)   
      {   
      tempName = pro.Name;   
      if (table.Columns.Contains(tempName))   
      {   
      object value = row[tempName];   
      pro.SetValue(t, value, null);   
      }   
      }   
      list.Add(t);   
      }   
      return list;   
      }   能否给一个调用的例子呀!感谢!!