判断是否有数据, 是否转换为string, 是否是Nullable
private static List<T> DataToModel<T>(DataSet ds) {
DataTable dt = ds.Tables [0];
try {
List<T> modelList = new List<T>();
if (dt == null || dt.Rows.Count == 0) {
return modelList;
} else if (typeof(T) == typeof(string)) { // T是 string类型
modelList = dt.AsEnumerable().Select(x => x.Field<T>(dt.Columns [0].ColumnName)).ToList();
} else {
//foreach ( DataRow dr in ds.Tables [0].Rows ) {
for (int r = 0; r < ds.Tables [0].Rows.Count; r++) {
DataRow dr = ds.Tables [0].Rows [r];
T model = ( T ) Activator.CreateInstance(typeof(T));
for (int i = 0; i < dr.Table.Columns.Count; i++) {
PropertyInfo propertyInfo = model.GetType().GetProperty(dr.Table.Columns [i].ColumnName);
if (propertyInfo != null && dr [i] != DBNull.Value) {
Type Typeof = null; //这里是获取实体类字段属性
if (propertyInfo.PropertyType.IsGenericType && propertyInfo.PropertyType.GetGenericTypeDefinition() == typeof(Nullable<>)) {
// If it is NULLABLE, then get the underlying type. eg if "Nullable<int>" then this will return just "int"
Typeof = propertyInfo.PropertyType.GetGenericArguments() [0];
} else {
Typeof = propertyInfo.PropertyType;
}
propertyInfo.SetValue(model, Convert.ChangeType(dr [i], Typeof), null); //然后用 changetype方法进行转换,因为dr[i]获取到的都是string类型,需要进行转换
}
}
modelList.Add(model);
}
}
return modelList;
} catch (Exception e) {
throw e;
}
}
#region 表/视图模型类转换
public V ViewToTable<K, V> ( K view, V table ) {
Type viewType = view.GetType();
Type tableType = table.GetType();
foreach (var tableInfo in tableType.GetProperties()) {
foreach (var viewInfo in viewType.GetProperties()) {
if (tableInfo.Name == viewInfo.Name) {
tableInfo.SetValue( table, viewInfo.GetValue( view, null ), null );
}
}
}
return table;
}
#endregion
#region DataTable 转换成 实体类
/// <summary>
/// 填充对象列表:用DataSet的第一个表填充实体类
/// </summary>
/// <param name="ds">DataSet</param>
/// <returns></returns>
public List<T> FillModel ( DataSet ds ) {
if (ds == null || ds.Tables[0] == null || ds.Tables[0].Rows.Count == 0) {
return new List<T>();
} else {
return FillModel( ds.Tables[0] );
}
}
/// <summary>
/// 填充对象列表:用DataSet的第index个表填充实体类
/// </summary>
public List<T> FillModel ( DataSet ds, int index ) {
if (ds == null || ds.Tables.Count <= index || ds.Tables[index].Rows.Count == 0) {
return null;
} else {
return FillModel( ds.Tables[index] );
}
}
/// <summary>
/// 填充对象列表:用DataTable填充实体类
/// </summary>
public List<T> FillModel ( DataTable dt ) {
if (dt == null || dt.Rows.Count == 0) {
return null;
}
List<T> modelList = new List<T>();
foreach (DataRow dr in dt.Rows) {
T model = (T)Activator.CreateInstance( typeof( T ) );
//T model = new T();
for (int i = 0; i < dr.Table.Columns.Count; i++) {
PropertyInfo propertyInfo = model.GetType().GetProperty( dr.Table.Columns[i].ColumnName );
if (propertyInfo != null && dr[i] != DBNull.Value) {
Type Typeof = null; //这里是获取实体类字段属性
if (propertyInfo.PropertyType.IsGenericType && propertyInfo.PropertyType.GetGenericTypeDefinition() == typeof( Nullable<> )) {
// If it is NULLABLE, then get the underlying type. eg if "Nullable<int>" then this will return just "int"
Typeof = propertyInfo.PropertyType.GetGenericArguments()[0];
} else {
Typeof = propertyInfo.PropertyType;
}
propertyInfo.SetValue( model, Convert.ChangeType( dr[i], Typeof ), null ); //然后用 changetype方法进行转换,因为dr[i]获取到的都是string类型,需要进行转换
}
}
modelList.Add( model );
}
return modelList;
}
/// <summary>
/// 填充对象:用DataRow填充实体类
/// </summary>
public T FillModel ( DataRow dr ) {
if (dr == null) {
return default( T );
}
T model = (T)Activator.CreateInstance( typeof( T ) );
//T model = new T();
for (int i = 0; i < dr.Table.Columns.Count; i++) {
PropertyInfo propertyInfo = model.GetType().GetProperty( dr.Table.Columns[i].ColumnName );
if (propertyInfo != null && dr[i] != DBNull.Value)
propertyInfo.SetValue( model, dr[i], null );
}
return model;
}
#endregion
#region 实体类转换成DataTable
/// <summary>
/// 实体类转换成DataSet
/// </summary>
/// <param name="modelList">实体类列表</param>
/// <returns></returns>
public DataSet FillDataSet ( List<T> modelList ) {
if (modelList == null || modelList.Count == 0) {
return null;
} else {
DataSet ds = new DataSet();
ds.Tables.Add( FillDataTable( modelList ) );
return ds;
}
}
/// <summary>
/// 实体类转换成DataTable
/// </summary>
/// <param name="modelList">实体类列表</param>
/// <returns></returns>
public DataTable FillDataTable ( List<T> modelList ) {
if (modelList == null || modelList.Count == 0) {
return null;
}
DataTable dt = CreateData( modelList[0] );
foreach (T model in modelList) {
DataRow dataRow = dt.NewRow();
foreach (PropertyInfo propertyInfo in typeof( T ).GetProperties()) {
dataRow[propertyInfo.Name] = propertyInfo.GetValue( model, null );
}
dt.Rows.Add( dataRow );
}
return dt;
}
/// <summary>
/// 根据实体类得到表结构
/// </summary>
/// <param name="model">实体类</param>
/// <returns></returns>
private DataTable CreateData ( T model ) {
DataTable dataTable = new DataTable( typeof( T ).Name );
foreach (PropertyInfo propertyInfo in typeof( T ).GetProperties()) {
dataTable.Columns.Add( new DataColumn( propertyInfo.Name, propertyInfo.PropertyType ) );
}
return dataTable;
}
#endregion
参考:
http://blog.csdn.net/caowei880123/article/details/6327412
http://www.cnblogs.com/dyfzwj/archive/2011/04/16/2017916.html
更详细介绍:http://www.cnblogs.com/HCCZX/archive/2012/07/26/2609389.html