List转换DataTable
- /// <summary>
- /// 将泛类型集合List类转换成DataTable
- /// </summary>
- /// <param name="list">泛类型集合</param>
- /// <returns></returns>
- public static DataTable ListToDataTable<T>(List<T> entitys)
- {
- //检查实体集合不能为空
- if (entitys == null || entitys.Count < 1)
- {
- throw new Exception("需转换的集合为空");
- }
- //取出第一个实体的所有Propertie
- Type entityType = entitys[0].GetType();
- PropertyInfo[] entityProperties = entityType.GetProperties();
- //生成DataTable的structure
- //生产代码中,应将生成的DataTable结构Cache起来,此处略
- DataTable dt = new DataTable();
- 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;
- }
List转换DataTable的更多相关文章
- dataGrid转换dataTable
#region dataGrid转换dataTable /// <summary> /// dataGrid转换dataTable /// </summary> ...
- C# 将Excel以文件流转换DataTable
/* *引用 NuGet包 Spire.XLS */ /// <summary> /// Excel帮助类 /// </summary> public class ExcelH ...
- List<T> 转换 DataTable
public class List2DataTable { public static string GetClassName(Type type) { if (typ ...
- DataRow数组转换DataTable
public DataTable ToDataTable(DataRow[] rows) { if (rows == null || rows.Length == 0) return null; Da ...
- C# DataRow[]转换DataTable
DataTable dt = ... DataRow[] dr = dt.Select("ID=14"); dt = dr.CopyToDataTable();
- c# 将csv文件转换datatable的两种方式。
第一种: public static DataTable csvdatatable(string path) { DataTable dt = new DataTable(); string conn ...
- 实体lis<T>t转换datatable
public static DataTable ListToTable<T>(List<T> list) { Type type = typeof(T) ...
- 读CSV转换datatable
using System.Data; using System.IO; /// <summary> /// Stream读取.csv文件 /// </summary> // ...
- SqL读取XML、解析XML、SqL将XML转换DataTable、SqL将XML转换表
DECLARE @ItemMessage XML )) SET @ItemMessage=N' <ReceivablesInfos> <ReceivablesList> < ...
随机推荐
- 单点登录(SSO)系统的总结
前些天一位其他开发部门的同事找到我们了解一些关于SSO单点登录的事,他们要做单点登录,同时也需要和我们这边的系统做集成,要我帮忙做一单点登录,了解关于单点登录的解决方案和资料,虽然做单点登录已经很久了 ...
- php常用时间戳记录
<?php echo '<br/>'; //php获取今日开始时间戳和结束时间戳 echo "今天"; echo '<br/>'; $beginTod ...
- python 选择排序
选择排序算法的思想,首先第一次先从整个序列中选择最小的数,然后放到第一位,然后再从第二位到最后一位选择出最小的一个数,把这个数放到第二位,然后,再从第三位到最后一位选择其中最小的数放到第三位,这样一直 ...
- Nginx for Windows 使用笔记
一.常见启动错误 1. "No mapping for the Unicode character exists in the target multi-byte code page&quo ...
- Junit使用
eclipse Junit的简单使用: eclipse自带了Junit插件. 安装Junit,如下图所示,右键项目-->Properties-->Add Library 选择Junit 选 ...
- SQl语句学习笔记(二)
merge into when matched then... when not mached then... merge into t_road_pre_parameter a fr ...
- solr6.1-----mysql 数据导入-查询
此部分一定要细心,lz 中间错了一个细节,调了好长时间(汗).请严格按照步骤操作 新建core 步骤1: 在webapps中solrhome下新建一个文件夹名字叫做collection1(名字不固定, ...
- Sql导出数据报错-->SQL Server 阻止了对组件 'Ad Hoc Distributed Queries' 的 STATEMENT'OpenRowset/OpenDatasource' 的访问
SQL Server 阻止了对组件 'Ad Hoc Distributed Queries' 的 STATEMENT'OpenRowset/OpenDatasource' 的访问,因为此组件已作为此服 ...
- Linux 中常见的命令行,持续更新
1.添加自己的环境变量 root@adonis:~# echo $PATH /usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin r ...
- jquery隐藏按钮
$(function () { jhbs = getQueryStringByName('jhbs'); shhbs = getQueryStringByName('shhbs'); if (shhb ...