/// <summary>
/// The data extension.
/// </summary>
public static class DataExtension
{
/// <summary>
/// ToList
/// </summary>
/// <typeparam name="T">T</typeparam>
/// <param name="reader">reader</param>
/// <returns>T</returns>
public static List<T> ToList<T>(this IDataReader reader) where T : class, new()
{
var result = new List<T>(); DataTable dt = reader.GetSchemaTable();
try
{
while (reader.Read())
{
var t = new T(); if (dt != null)
{
foreach (DataRow dr in dt.Rows)
{
// 当前列名&属性名
string columnName = dr[].ToString();
PropertyInfo pro = typeof(T).GetProperty(columnName); if (pro == null)
{
continue;
} if (!pro.CanWrite)
{
continue;
} pro.SetValue(t, ConvertExtension.ConvertHelper(reader[columnName], pro.PropertyType), null);
}
} result.Add(t);
}
}
catch (System.Exception ex)
{
throw ex;
}
finally
{
if (!reader.IsClosed)
{
reader.Dispose();
reader.Close();
}
} return result;
} /// <summary>
/// ToList
/// </summary>
/// <typeparam name="T">T</typeparam>
/// <param name="dt">dt</param>
/// <returns>T</returns>
public static List<T> ToList<T>(this DataTable dt) where T : class, new()
{
var result = new List<T>();
foreach (DataRow dr in dt.Rows)
{
var t = new T();
try
{
foreach (DataColumn column in dt.Columns)
{
// 当前列名&属性名
string columnName = column.ColumnName;
PropertyInfo pro = typeof(T).GetProperty(columnName); if (pro == null)
{
continue;
} if (!pro.CanWrite)
{
continue;
} pro.SetValue(t, ConvertExtension.ConvertHelper(dr[columnName], pro.PropertyType), null);
}
}
catch (System.Exception ex)
{
throw ex;
} result.Add(t);
} return result;
} /// <summary>
/// ToList
/// </summary>
/// <typeparam name="T">T</typeparam>
/// <param name="ds">ds</param>
/// <returns>T</returns>
public static List<T> ToList<T>(this DataSet ds) where T : class, new()
{
return ds.Tables[].ToList<T>();
} /// <summary>
/// ToList
/// </summary>
/// <typeparam name="T">T</typeparam>
/// <param name="ds">ds</param>
/// <param name="dataTableIndex">dataTableIndex</param>
/// <returns>T</returns>
public static List<T> ToList<T>(this DataSet ds, int dataTableIndex) where T : class, new()
{
return ds.Tables[dataTableIndex].ToList<T>();
} /// <summary>
/// ToModel
/// </summary>
/// <typeparam name="T">T</typeparam>
/// <param name="reader">reader</param>
/// <returns>T</returns>
public static T ToModel<T>(this IDataReader reader) where T : class, new()
{
var t = new T();
DataTable dt = reader.GetSchemaTable();
try
{
while (reader.Read())
{
if (dt != null)
{
foreach (DataRow dr in dt.Rows)
{
// 当前列名&属性名
string columnName = dr[].ToString();
PropertyInfo pro = typeof(T).GetProperty(columnName); if (pro == null)
{
continue;
} if (!pro.CanWrite)
{
continue;
} pro.SetValue(t, ConvertExtension.ConvertHelper(reader[columnName], pro.PropertyType), null);
}
}
}
}
catch (System.Exception ex)
{
throw ex;
}
finally
{
if (!reader.IsClosed)
{
reader.Dispose();
reader.Close();
}
} return t;
} /// <summary>
/// ToModel
/// </summary>
/// <typeparam name="T">T</typeparam>
/// <param name="dt">dt</param>
/// <returns>T</returns>
public static T ToModel<T>(this DataTable dt) where T : class, new()
{
var t = new T();
if (dt.Rows.Count <= )
{
return t;
} try
{
foreach (DataColumn column in dt.Columns)
{
// 当前列名&属性名
string columnName = column.ColumnName;
PropertyInfo pro = typeof(T).GetProperty(columnName);
if (pro == null)
{
continue;
} if (!pro.CanWrite)
{
continue;
} pro.SetValue(t, ConvertExtension.ConvertHelper(dt.Rows[][columnName], pro.PropertyType), null);
}
}
catch (System.Exception ex)
{
throw ex;
} return t;
} /// <summary>
/// ToModel
/// </summary>
/// <typeparam name="T">T</typeparam>
/// <param name="ds">ds</param>
/// <param name="dataTableIndex">dataTableIndex</param>
/// <returns>T</returns>
public static T ToModel<T>(this DataSet ds, int dataTableIndex = ) where T : class, new()
{
return ds.Tables[].ToModel<T>();
}
}

DataExtension

     /// <summary>
/// The convert extension.
/// </summary>
public static class ConvertExtension
{
/// <summary>
/// The convert helper.
/// </summary>
/// <param name="value">
/// The value.
/// </param>
/// <param name="conversionType">
/// The conversion type.
/// </param>
/// <returns>
/// The <see cref="object"/>.
/// </returns>
public static object ConvertHelper(object value, Type conversionType)
{
Type nullableType = Nullable.GetUnderlyingType(conversionType); // 判断当前类型是否可为 null
if (nullableType != null)
{
if (value == DBNull.Value)
{
return null;
} // 若是枚举 则先转换为枚举
if (nullableType.IsEnum)
{
value = System.Enum.Parse(nullableType, value.ToString());
} return Convert.ChangeType(value, nullableType);
} if (conversionType.IsEnum)
{
return System.Enum.Parse(conversionType, value.ToString());
} return Convert.ChangeType(value, conversionType);
} /// <summary>
/// The convert to decimal null.
/// </summary>
/// <param name="targetObj">
/// The target obj.
/// </param>
/// <returns>
/// The <see cref="decimal"/>.
/// </returns>
public static decimal? ConvertToDecimalNull(object targetObj)
{
if (targetObj == null || targetObj == DBNull.Value)
{
return null;
} return Convert.ToDecimal(targetObj);
} /// <summary>
/// The convert to int null.
/// </summary>
/// <param name="targetObj">
/// The target obj.
/// </param>
/// <returns>
/// The <see cref="int"/>.
/// </returns>
public static int? ConvertToIntNull(object targetObj)
{
if (targetObj == null || targetObj == DBNull.Value)
{
return null;
} return Convert.ToInt32(targetObj);
} /// <summary>
/// The convert to string.
/// </summary>
/// <param name="obj">
/// The obj.
/// </param>
/// <returns>
/// The <see cref="string"/>.
/// </returns>
public static string ConvertToString(object obj)
{
return obj == null ? string.Empty : obj.ToString();
} /// <summary>
/// 将泛类型集合List类转换成DataTable
/// </summary>
/// <param name="entitys">泛类型集合</param>
/// <typeparam name="T">T</typeparam>
/// <returns>DataTable</returns>
public static DataTable ListToDataTable<T>(List<T> entitys)
{
// 检查实体集合不能为空
if (entitys == null || entitys.Count < )
{
throw new System.Exception("需转换的集合为空");
} // 取出第一个实体的所有Propertie
Type entityType = entitys[].GetType();
PropertyInfo[] entityProperties = entityType.GetProperties(); // 生成DataTable的structure
// 生产代码中,应将生成的DataTable结构Cache起来,此处略
DataTable dt = new DataTable();
foreach (PropertyInfo t in entityProperties)
{
// dt.Columns.Add(entityProperties[i].Name, entityProperties[i].PropertyType);
dt.Columns.Add(t.Name);
} // 将所有entity添加到DataTable中
foreach (object entity in entitys)
{
// 检查所有的的实体都为同一类型
if (entity.GetType() != entityType)
{
throw new System.Exception("要转换的集合元素类型不一致");
} object[] entityValues = new object[entityProperties.Length];
for (int i = ; i < entityProperties.Length; i++)
{
entityValues[i] = entityProperties[i].GetValue(entity, null);
} dt.Rows.Add(entityValues);
} return dt;
} /// <summary>
/// 转换中文星期
/// </summary>
/// <param name="dt">The dt.</param>
/// <returns>Week.</returns>
public static Week ConverToWeekByZHCN(this DateTime dt)
{
return (Week)dt.DayOfWeek;
} /// <summary>
/// 四舍五入保留2位小数(中国式)
/// </summary>
/// <param name="d"></param>
/// <returns></returns>
public static decimal DecimalTwoPlaces(this decimal d)
{
return Math.Round(d, , MidpointRounding.AwayFromZero);
}
}

ConvertExtension

将 IDataReader DataSet DataTable 转化成 List<T> Or T类型
上面是转化代码

最新文章

  1. winform记事本(基本功能)
  2. XML基础学习01
  3. BHP编译器教程
  4. IO流的练习4 —— 键盘录入学生成绩信息,进行排序后存入文本中
  5. Map(双列集合)
  6. bzoj 1015 并查集
  7. Leetcode#59 Spiral Matrix II
  8. mysql 存储过程 事务; mysql的事务中包含一个存储过程
  9. 《Python基础教程(第二版)》学习笔记 -&gt; 第四章 字典
  10. Content-Disposition的使用方法
  11. spring boot学习资源
  12. 使用laravel-admin后台sdk报错Failed to load resource: net::ERR_CERT_AUTHORITY_INVALID、Provisional headers are shown
  13. SpringMVC 使用 MultipartFile 实现文件上传
  14. 基于tensorflow的逻辑分类
  15. Spring 系列目录
  16. 包含jdk和nginx的基础镜像
  17. iOS 一些常见问题的整理
  18. Rest Client插件简单介绍
  19. vue的饿了么写作感受
  20. laravel 单词

热门文章

  1. iOS 网易彩票-3常见设置
  2. 单利模式及python实现方式
  3. Twitter OA prepare: K-complementary pair
  4. 一个新人对HTML的理解
  5. swoole线程和进程
  6. #C++初学记录(深度搜索#递归)
  7. CAScrollLayer
  8. Linux命令: 替换字符串
  9. Hive 常用函数汇总
  10. 一起学koa