zoukankan      html  css  js  c++  java
  • DataTable/DataRow转对象扩展方法

    目前的潮流应该都是使用各种ORM框架,但是个人在实际小项目中还是比较喜欢直接使用sql查询语句,再生成对象,那么就需要一个将查询结果转换成实体对象的通用方法,所以就有了这个扩展,已用了很久,总体来说还是比较方便的。

      1  public static class DataExtensions
      2     {
      3         /// <summary>
      4         /// DataRow扩展方法:将DataRow类型转化为指定类型的实体
      5         /// </summary>
      6         /// <typeparam name="T">实体类型</typeparam>
      7         /// <returns></returns>
      8         public static T ToModel<T>(this DataRow dr) where T : class, new()
      9         {
     10             return ToModel<T>(dr, true);
     11         }
     12         /// <summary>
     13         /// DataRow扩展方法:将DataRow类型转化为指定类型的实体
     14         /// </summary>
     15         /// <typeparam name="T">实体类型</typeparam>
     16         /// <param name="dateTimeToString">是否需要将日期转换为字符串,默认为转换,值为true</param>
     17         /// <returns></returns>
     18         /// <summary>
     19         public static T ToModel<T>(this DataRow dr, bool dateTimeToString) where T : class, new()
     20         {
     21             if (dr != null)
     22                 return ToList<T>(dr.Table, dateTimeToString).First();
     23             return null;
     24         }
     25         /// <summary>
     26         /// DataTable扩展方法:将DataTable类型转化为指定类型的实体集合
     27         /// </summary>
     28         /// <typeparam name="T">实体类型</typeparam>
     29         /// <returns></returns>
     30         public static List<T> ToList<T>(this DataTable dt) where T : class, new()
     31         {
     32             return ToList<T>(dt, true);
     33         }
     34         /// <summary>
     35         /// DataTable扩展方法:将DataTable类型转化为指定类型的实体集合
     36         /// </summary>
     37         /// <typeparam name="T">实体类型</typeparam>
     38         /// <param name="dateTimeToString">是否需要将日期转换为字符串,默认为转换,值为true</param>
     39         /// <returns></returns>
     40         public static List<T> ToList<T>(this DataTable dt, bool dateTimeToString) where T : class, new()
     41         {
     42             List<T> list = new List<T>();
     43 
     44             if (dt != null)
     45             {
     46                 List<PropertyInfo> infos = new List<PropertyInfo>();
     47                 Array.ForEach<PropertyInfo>(typeof(T).GetProperties(), p =>
     48                 {
     49                     if (dt.Columns.Contains(p.Name) == true)
     50                     {
     51                         infos.Add(p);
     52                     }
     53                 });
     54                 SetList<T>(list, infos, dt, dateTimeToString);
     55             }
     56             return list;
     57         }
     58         #region 私有方法
     59         private static void SetList<T>(List<T> list, List<PropertyInfo> infos, DataTable dt, bool dateTimeToString) where T : class, new()
     60         {
     61             foreach (DataRow dr in dt.Rows)
     62             {
     63                 T model = new T();
     64                 infos.ForEach(p =>
     65                 {
     66                     if (dr[p.Name] != DBNull.Value)
     67                     {
     68                         object tempValue = dr[p.Name];
     69                         if (dr[p.Name].GetType() == typeof(DateTime) && dateTimeToString == true)
     70                         {
     71                             tempValue = dr[p.Name].ToString();
     72                         }
     73 
     74                         if (p.PropertyType==typeof(List<string>))//增加直接将json转为List<string>,存储结构为["str1","str2","str3"...]
     75                         {
     76                             tempValue = JsonConvert.DeserializeObject<List<string>>(dr[p.Name].ToString());
     77                         }
     78 
     79                         try
     80                         {
     81                             p.SetValue(model, tempValue, null);
     82                         }
     83                         catch (Exception e)
     84                         {
     85                             Console.WriteLine(e.Message);
     86                         }
     87                     }
     88                     else {//DBNull值直接转成空字符串
     89                         try
     90                         {
     91                             p.SetValue(model, "", null);
     92                         }
     93                         catch { }
     94                     }
     95                 });
     96                 list.Add(model);
     97             }
     98         }
     99         #endregion
    100     }
  • 相关阅读:
    备库归档重新传命令
    DG 参数详解
    ACTIVATE STANDBY
    Matplotlib模块:绘图和可视化
    Django总结一
    Django之用户认证—auth模块
    最长上升子序列
    Pandas模块:表计算与数据分析
    Django ORM操作

  • 原文地址:https://www.cnblogs.com/xiaoyu5062/p/9673213.html
Copyright © 2011-2022 走看看