C# DataTable与Model互转的示例代码

作者:李志强 时间:2022-02-07 19:37:44 


/// <summary>
/// 实体转换辅助类
/// </summary>
public class ModelConvertHelper<T> where T : new()
{
 /// <summary>
 /// List泛型转换DataTable.
 /// </summary>
 public DataTable ListToDataTable<T>(List<T> items)
 {
  var tb = new DataTable(typeof(T).Name);

PropertyInfo[] props = typeof(T).GetProperties(BindingFlags.Public | BindingFlags.Instance);

foreach (PropertyInfo prop in props)
  {
   Type t = GetCoreType(prop.PropertyType);
   tb.Columns.Add(prop.Name, t);
  }

foreach (T item in items)
  {
   var values = new object[props.Length];

for (int i = 0; i < props.Length; i++)
   {
    values[i] = props[i].GetValue(item, null);
   }

tb.Rows.Add(values);
  }

return tb;
 }

/// <summary>
 /// model转换DataTable
 /// </summary>
 /// <typeparam name="T"></typeparam>
 /// <param name="items"></param>
 /// <returns></returns>
 public DataTable ModelToDataTable<T>(T items)
 {
  var tb = new DataTable(typeof(T).Name);

PropertyInfo[] props = typeof(T).GetProperties(BindingFlags.Public | BindingFlags.Instance);

foreach (PropertyInfo prop in props)
  {
   Type t = GetCoreType(prop.PropertyType);
   tb.Columns.Add(prop.Name, t);
  }

var values = new object[props.Length];

for (int i = 0; i < props.Length; i++)
  {
   values[i] = props[i].GetValue(items, null);
  }

tb.Rows.Add(values);

return tb;
 }

/// <summary>
 /// Determine of specified type is nullable
 /// </summary>
 public static bool IsNullable(Type t)
 {
  return !t.IsValueType || (t.IsGenericType && t.GetGenericTypeDefinition() == typeof(Nullable<>));
 }

/// <summary>
 /// Return underlying type if type is Nullable otherwise return the type
 /// </summary>
 public static Type GetCoreType(Type t)
 {
  if (t != null && IsNullable(t))
  {
   if (!t.IsValueType)
   {
    return t;
   }
   else
   {
    return Nullable.GetUnderlyingType(t);
   }
  }
  else
  {
   return t;
  }
 }

/// <summary>
 /// DataTable转换泛型List
 /// </summary>
 /// <param name="dt"></param>
 /// <returns></returns>
 public static List<T> DataTableToList(DataTable dt)
 {
  // 定义集合
  List<T> ts = new List<T>();

// 获得此模型的类型
  Type type = typeof(T);
  string tempName = "";
  foreach (DataRow dr in dt.Rows)
  {
   T t = new T();
   // 获得此模型的公共属性
   PropertyInfo[] propertys = t.GetType().GetProperties();
   foreach (PropertyInfo pi in propertys)
   {
    tempName = pi.Name; // 检查DataTable是否包含此列

if (dt.Columns.Contains(tempName))
    {
     // 判断此属性是否有Setter
     if (!pi.CanWrite) continue;

object value = dr[tempName];
     if (value != DBNull.Value)
      pi.SetValue(t, value, null);
    }
   }
   ts.Add(t);
  }
  return ts;
 }

public static T DataTableToModel(DataTable dt)
 {
  // 定义实体
  T t = new T();

// 获得此模型的类型
  Type type = typeof(T);
  string tempName = "";

foreach (DataRow dr in dt.Rows)
  {

// 获得此模型的公共属性
   PropertyInfo[] propertys = t.GetType().GetProperties();
   foreach (PropertyInfo pi in propertys)
   {
    tempName = pi.Name; // 检查DataTable是否包含此列

if (dt.Columns.Contains(tempName))
    {
     // 判断此属性是否有Setter
     if (!pi.CanWrite) continue;

object value = dr[tempName];
     if (value != DBNull.Value)
      pi.SetValue(t, value, null);
    }
   }
   break;
  }
  return t;
 }
}

来源:https://www.cnblogs.com/stulzq/p/6137780.html

标签:C#,DataTable,Model
0
投稿

猜你喜欢

  • Springboot使用redis实现接口Api限流的示例代码

    2023-11-29 02:11:05
  • idea 模板编程知识小结

    2022-06-22 13:18:34
  • C#装箱和拆箱的原理介绍

    2022-06-18 02:33:17
  • SpringMVC自定义拦截 器登录检测功能的实现代码

    2023-07-27 18:33:05
  • 解决MyEclipse中的Building workspace问题的三个方法

    2023-10-25 09:00:51
  • C#推送信息到APNs的方法

    2023-05-29 05:20:59
  • 最详细的文件上传下载实例详解(推荐)

    2021-12-12 08:18:13
  • C#实现异步的常用方式总结

    2023-10-26 17:25:13
  • SpringCloudConfig之client端报错Could not resolve placeholder问题

    2023-11-23 11:19:17
  • spring data jpa使用详解(推荐)

    2022-06-19 05:22:17
  • C#多线程开发实战记录之线程基础

    2022-11-03 03:21:45
  • 浅谈关于Mybatis的mapper-locations配置问题

    2023-09-24 06:06:16
  • 详解Java目录操作与文件操作教程

    2023-12-05 13:45:48
  • Java获取用户IP属地模拟抖音详解

    2023-04-18 02:01:29
  • Java Comparable 和 Comparator 的详解及区别

    2023-07-05 10:41:44
  • Java验证时间格式是否正确方法类项目实战

    2021-05-30 02:15:15
  • Java 精炼解读数据结构逻辑控制

    2023-09-14 05:35:37
  • Java SpringBoot实现文件上传功能的示例代码

    2022-05-23 02:54:46
  • Java中JUC 的 Exchange 交换器详情

    2023-09-17 18:46:40
  • C# Websocket连接实现wss协议

    2022-09-02 08:20:41
  • asp之家 软件编程 m.aspxhome.com