//list转化为table
public static DataTable ListToDataTable<T>(List<T> entitys)
{ //检查实体集合不能为空
if (entitys == null || entitys.Count < 1)
{
return new DataTable();
} //取出第一个实体的所有Propertie
Type entityType = entitys[0].GetType();
PropertyInfo[] entityProperties = entityType.GetProperties(); //生成DataTable的structure
//生产代码中,应将生成的DataTable结构Cache起来,此处略
DataTable dt = new DataTable("dt");
for (int i = 0; i < entityProperties.Length; i++)
{
//dt.Columns.Add(entityProperties[i].Name, entityProperties[i].PropertyType);
dt.Columns.Add(entityProperties[i].Name);
} //将所有entity添加到DataTable中
foreach (object entity in entitys)
{
//检查所有的的实体都为同一类型
if (entity.GetType() != entityType)
{
throw new Exception("要转换的集合元素类型不一致");
}
object[] entityValues = new object[entityProperties.Length];
for (int i = 0; i < entityProperties.Length; i++)
{
entityValues[i] = entityProperties[i].GetValue(entity, null); }
dt.Rows.Add(entityValues);
}
return dt;
}

C# list与table的互转的更多相关文章

  1. lua中,两种json和table互转方法的效率比较

    lua中json和table的互转,是我们在平时开发过程中经常用到的.比如: 在用lua编写的服务器中,如果客户端发送json格式的数据,那么在lua处理业务逻辑的时候,必然需要转换成lua自己的数据 ...

  2. c#结构体、打他table、excel、csv互转

    1.csv相关 public static class CsvHelper { /// <summary> /// 根据csv路径获取datatable /// </summary& ...

  3. table与json的互转

    json是键值对,在Lua中类型是string 主要运用在table中.表:local t={a="1",b="2",c="3",d=&qu ...

  4. 推荐Html Table和Markown互转的网站Table Convert Online

    网站名称:https://tableconvert.com/ 进入网站可以看到可以Table 转为Markdown.JSON.XML.SQL 多种格式 Table(4×5)定义Table的行数和列数: ...

  5. 符号表 symbol table 符号 地址 互推

    https://zh.wikipedia.org/wiki/符号表 https://en.wikipedia.org/wiki/Symbol_table 在计算机科学中,符号表是一种用于语言翻译器(例 ...

  6. SQL2008使用json.net实现XML与JSON互转

    借助CLR,首先实现字符串的互转,然后使用存储过程实现JSON2table     public class JsonFunction    {        /// <summary> ...

  7. UVa10820 Send a Table[欧拉函数]

    Send a TableInput: Standard Input Output: Standard Output When participating in programming contests ...

  8. [转载] 散列表(Hash Table)从理论到实用(中)

    转载自:白话算法(6) 散列表(Hash Table)从理论到实用(中) 不用链接法,还有别的方法能处理碰撞吗?扪心自问,我不敢问这个问题.链接法如此的自然.直接,以至于我不敢相信还有别的(甚至是更好 ...

  9. 纵表、横表互转的SQL

    纵表.横表互转的SQL By:大志若愚 1.建表: 纵表结构 Table_A  create table Table_A ( 姓名 ), 课程 ), 成绩 int ) ) ) ) ) ) 姓名 课程 ...

随机推荐

  1. iOS UIWebView URL拦截

    http://www.cocoachina.com/ios/20150626/12161.html 本文译者:candeladiao,原文:URL filtering for UIWebView on ...

  2. @loj - 2091@ 「ZJOI2016」小星星

    目录 @description@ @solution@ @accepted code@ @details@ @description@ 小 Y 是一个心灵手巧的女孩子,她喜欢手工制作一些小饰品.她有 ...

  3. selenium 自动化点击页面

    #!/usr/bin/env python# -*- coding:utf-8 -*-from selenium import webdriverfrom selenium.webdriver.com ...

  4. 云数据库 MySQL 8.0 重磅发布,更适合企业使用场景的RDS数据库

    点击订阅新品发布会! 新产品.新版本.新技术.新功能.价格调整,评论在下方,下期更新!关注更多内容,了解更多 最新发布 云数据库MySQL 8.0 升级发布会 2019年5月29日15时,阿里云云数据 ...

  5. oracle sum()聚合函数

    原文链接:https://blog.csdn.net/cizatu5130/article/details/100291347 oracle sum()聚合函数 2016-05-13 20:08:00 ...

  6. Python--day63--添加书籍和修改表结构的注意事项

  7. java TreeSet的排序之定制排序

    TreeSet的自然排序是根据元素的大小进行升序排序的,若想自己定制排序,比如降序排序,就可以使用Comparator接口了: 该接口包含int compare(Object o1,Object o2 ...

  8. 2019-8-31-C#-控制台使用-UAC-权限

    title author date CreateTime categories C# 控制台使用 UAC 权限 lindexi 2019-08-31 16:55:58 +0800 2018-07-05 ...

  9. keep-alive及路由渲染

    切换路由的时候,每次切换的时候得重新渲染一遍,这样的话会影响到性能的.此时用<keep-alive>包裹着app里的<router-view>,进行缓存. 如果一个页面涉及到了 ...

  10. Jasypt加密SpringBoot配置文件

    如果 SpringBoot 的 properties 文件中含有用户名密码等敏感信息,为了安全起见需要对明文密码加密.Jasypt 是用来加密的 jar 包. 1.引入 Jasypt 在 pom.xm ...