博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
ModelConvertHelper(将DataTable转换成List<model>)
阅读量:4630 次
发布时间:2019-06-09

本文共 700 字,大约阅读时间需要 2 分钟。

 

public class ModelConvertHelper<T> where T : new()

{
     public static IList<T> ConvertToModel(DataTable dt)
     {
         // Collection definition
         IList<T> ts = new List<T>();

         // Get model type

         Type type = typeof(T);

         string tempName = "";

         foreach (DataRow dr in dt.Rows)

         {
             T t = new T();

             // Get property of model

             PropertyInfo[] propertys = t.GetType().GetProperties();

             foreach (PropertyInfo pi in propertys)

             {
                 tempName = pi.Name;

                 // check column is exsit

                 if (dt.Columns.Contains(tempName))
                 {
                     // Whether can be set value
                     if (!pi.CanWrite) continue;

                     object value = dr[tempName];

                     if (value != DBNull.Value)
                         pi.SetValue(t, value, null);
                 }
             }

             ts.Add(t);

         }

         return ts;

     }
}

转载于:https://www.cnblogs.com/key1309/p/3566567.html

你可能感兴趣的文章
WinForm 实现验证码
查看>>
[C++]C++中的IO类
查看>>
笔记本电脑(Windows7)实现无线AP
查看>>
JqGridView 1.0.0.0发布
查看>>
欲精一行,必先通十行
查看>>
前端相关html和css
查看>>
celery
查看>>
实现音乐播放器
查看>>
BZOJ1002 [FJOI2007]轮状病毒(最小生成树计数)
查看>>
uv_timer_t的释放问题
查看>>
【bzoj1853】[Scoi2010]幸运数字 容斥原理+搜索
查看>>
【bzoj2770】YY的Treap 权值线段树
查看>>
利用闭包实现多次ajax请求只执行最后一次
查看>>
任务18:控制反转
查看>>
MyEclipse10整合Axis2插件
查看>>
ORACLE触发器详解
查看>>
边工作边刷题:70天一遍leetcode: day 27
查看>>
BZOJ1702: [Usaco2007 Mar]Gold Balanced Lineup 平衡的队列
查看>>
Shell基础命令之echo
查看>>
windows 常用命令
查看>>