c#如何将数据表中是数据封装到实体对象里?实现思路上各位有什么想法和做法?非常感谢讨论。

解决方案 »

  1.   

    遍历数据集,添加到list<T>
    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.   

    model 这个... get属性() set属性()
      

  3.   

    List <实体类>实体类是自定义 跟表字段对应