首页 > 其他分享 >DataTable 与 泛型集合List<T>相互转换

DataTable 与 泛型集合List<T>相互转换

时间:2024-07-09 13:32:41浏览次数:10  
标签:List prop result 泛型 new DataTable row

List转DataTable
   public static DataTable ToDataTable<T>(this List<T> list)
    {
        DataTable result = new DataTable();
        List<PropertyInfo> pList = new List<PropertyInfo>();
        Type type = typeof(T);
        Array.ForEach<PropertyInfo>(type.GetProperties(), prop => { pList.Add(prop); result.Columns.Add(prop.Name, prop.PropertyType); });
        foreach (var item in list)
        {
            DataRow row = result.NewRow();
            pList.ForEach(p => row[p.Name] = p.GetValue(item, null));
            result.Rows.Add(row);
        }
        return result;
    }
DataTable转List
  public static List<T> ToList<T>(this DataTable table) where T : class, new()
    {
        List<T> result = new List<T>();
        List<PropertyInfo> pList = new List<PropertyInfo>();
        Type type = typeof(T);
        Array.ForEach<PropertyInfo>(type.GetProperties(), prop => { if (table.Columns.IndexOf(prop.Name) != -1) pList.Add(prop); });
        foreach (DataRow row in table.Rows)
        {
            T obj = new T();
            pList.ForEach(prop => { if (row[prop.Name] != DBNull.Value) prop.SetValue(obj, row[prop.Name], null); });
            result.Add(obj);
        }
        return result;
    }

标签:List,prop,result,泛型,new,DataTable,row
From: https://www.cnblogs.com/ksq1063/p/18291599

相关文章

  • TNS问题排查 The listener supports no services
     检查tns的日志信息查看具体报错详情/u01/app/oracle/diag/tnslsnr/<hostname>/listener/alert/log.xml 修改litener.ora #listener.oraNetworkConfigurationFile:/u01/app/oracle/product/11.2.0/dbhome_1/network/admin/listener.ora#GeneratedbyOracleco......
  • 【数据结构】—— 单链表(single linked list)
    文章目录1、单链表的定义优点和缺点单链表的构成2、单链表的创建初始化:3、单链表的接口实现打印尾插头插尾删头删查找在指定位置之前插入在指定位置之后插入删除指定位置的节点删除指定位置之后的节点销毁链表4、源代码1、单链表的定义单链表(SinglyLinkedList......
  • List 转 Page
    packagecom.leo.boot.jpa.stream;importorg.apache.commons.collections4.CollectionUtils;importorg.apache.commons.collections4.IterableUtils;importorg.springframework.beans.BeanUtils;importorg.springframework.data.domain.Page;importorg.springfram......
  • List 按照指定大小分片的几种方式
    如果有一个list<string>里面可能有1000份或者更多数据,如果需要进行入库等操作,需要拆分成指定大小每份进行处理,这种需求很常见,那么应该怎么做呢?首先我们需要将List<String> 转为多份后进行逐个处理, 处理批量事务注意事务哦 那么怎么将list转为多份呢? 下面介绍2......
  • java核心-泛型
    目录概述什么是泛型分类泛型类泛型接口泛型方法泛型通配符分类泛型类型擦除分类无限制类型擦除有限制类型擦除问题需求第一种第二种概述  了解泛型有利于学习jdk、中间件的源码,提升代码抽象能力,封装通用性更强的组件。什么是泛型在定义类、接口和方法时,......
  • 泛型
    泛型的本质就是泛型参数化,确保类型一致性和安全性泛型上限与泛型下限泛型上限和泛型下限都是用于限定参数范围的1.泛型上限(上限指的是上限范围,读取的上限范围,)通过extends关键字来限制参数上限,通过限制操作的顶层基类,来控制读取的类型,因为你读取的所有的对象,都是这个顶层基类的......
  • python数据容器(一)列表list
    思维导图代码1.数据容器入门2.数据容器:list(列表)name_list=['itheima','itcast','python']print(name_list)print(type(name_list))运行结果: name_list=['itheima',666,True]print(name_list)print(type(name_list))运行结果: name_l......
  • python: list
     #去重A=['geovindu','刘杰','江山','河水','刘杰','geovindu','张三','李五']B=[]foriinA:ifinotinB:B.append(i)print(B)C=set(A)......
  • HashTable,ArrayList,queue等常用方法
    HashTable,ArrayList,queue等常用方法HashMap是Java中非常常用的集合类,它存储键值对,并提供了一系列方便的方法来操作这些数据。以下是一些HashMap的常用方法:1.添加和获取元素:put(key,value):将指定的键值对添加到HashMap中。如果键已存在,则更新对应的值。get(ke......
  • C++list的模拟实现
    链表节点 template<classT> structListNode { ListNode(constT&data=T()) : _data(data) { } ListNode<T>*_prev=nullptr; ListNode<T>*_next=nullptr; T_data; };因为之后要访问这个类的成员变量函数和结构体,所以在这里将class直接改为struct......