陈程的技术博客

  • 关于作者
全栈软件工程师
一个专注于技术研究创新的程序员
  1. 首页
  2. .NET
  3. 正文

ListHelper帮助类,DataTable和List之间转换

2016年6月15日 559点热度 0人点赞 0条评论

ListHelper帮助类,DataTable和List之间转换

public class ListHelper
   {
       /// <summary>
       /// 将集合类转换成DataTable
       /// </summary>
       /// <param name="list">集合</param>
       /// <returns></returns>
       public static DataTable ToDataTable(IList list)
       {
           DataTable result = new DataTable();
           if (list.Count > 0)
           {
               PropertyInfo[] propertys = list[0].GetType().GetProperties();
               foreach (PropertyInfo pi in propertys)
               {
                   result.Columns.Add(pi.Name, pi.PropertyType);
               }

               for (int i = 0; i < list.Count; i++)
               {
                   ArrayList tempList = new ArrayList();
                   foreach (PropertyInfo pi in propertys)
                   {
                       object obj = pi.GetValue(list[i], null);
                       tempList.Add(obj);
                   }
                   object[] array = tempList.ToArray();
                   result.LoadDataRow(array, true);
               }
           }
           return result;
       }

       /// <summary>
       /// 指定数据实体转换为DataTable
       /// </summary>
       /// <typeparam name="T"></typeparam>
       /// <param name="t"></param>
       /// <returns></returns>
       public static DataTable ObjectToDataTable<T>(T t)
       {
           DataTable result = new DataTable();

           PropertyInfo[] propertys = t.GetType().GetProperties();
           foreach (PropertyInfo pi in propertys)
           {
               result.Columns.Add(pi.Name, pi.PropertyType);
           }

           ArrayList tempList = new ArrayList();
           foreach (PropertyInfo pi in propertys)
           {
               object obj = pi.GetValue(t, null);
               tempList.Add(obj);
           }
           object[] array = tempList.ToArray();
           result.LoadDataRow(array, true);


           return result;
       }

       /// <summary>
       /// 将集合类转换成DataTable,数据都转化为string类型
       /// </summary>
       /// <param name="list">集合</param>
       /// <returns></returns>
       public static DataTable ToDataTableAllString(IList list)
       {
           DataTable result = new DataTable();
           if (list.Count > 0)
           {
               PropertyInfo[] propertys = list[0].GetType().GetProperties();
               foreach (PropertyInfo pi in propertys)
               {
                   result.Columns.Add(pi.Name, typeof(String));
               }

               for (int i = 0; i < list.Count; i++)
               {
                   ArrayList tempList = new ArrayList();
                   foreach (PropertyInfo pi in propertys)
                   {
                       object obj = pi.GetValue(list[i], null);
                       tempList.Add(obj);
                   }
                   object[] array = tempList.ToArray();
                   result.LoadDataRow(array, true);
               }
           }
           return result;
       }
   }

 

标签: C#
最后更新:2021年4月1日

博主

全栈工程师,侧重项目技术解决方案规划和开发

打赏 点赞
< 上一篇
下一篇 >

文章评论

取消回复

分类
  • .NET (65)
  • docker (3)
  • linux (12)
  • python (20)
  • web (14)
  • 小程序 (4)
  • 数据库 (2)
  • 未分类 (4)
  • 杂七杂八 (10)
标签聚合
js nginx winform python C# centos linux DevExpress
最新 热点 随机
最新 热点 随机
.NET开发手册标准参考 招募兼职前端开发 Centos安装dotnet6环境 VS上切换分支,vs编译运行出现bug,A fatal error was encountered彻底解决方案 用C#封装一个线程安全的缓存器,达到目标定时定量更新入库 C#通过特性的方式去校验指定数据是否为空
WinForm使用简单的方法实现完美遮罩的效果 RestSharp 接口请求封装类 最简单的一种方法解决 Selenium上传文件 C# 压缩解压帮助类ZipHelper 小程序-云开发 获取用户的openid等信息 winform 微秒级别的定时器

COPYRIGHT © 2021 陈程的技术博客. ALL RIGHTS RESERVED.

THEME KRATOS MADE BY VTROIS