public static DataTable CopyToDataTable<T>(this IEnumerable<T> array)
{
var ret = new DataTable();
foreach (PropertyDescriptor dp in TypeDescriptor.GetProperties(typeof(T)))
{
//if (!dp.IsReadOnly)
{
ret.Columns.Add(dp.Name, dp.PropertyType);
}
}
foreach (T item in array)
{
var Row = ret.NewRow();
foreach (PropertyDescriptor dp in TypeDescriptor.GetProperties(typeof(T)))
{
//if (!dp.IsReadOnly)
{
Row[dp.Name] = dp.GetValue(item);
}
}
ret.Rows.Add(Row);
}
return ret;
}
static public DataTable ToDataTable<T>(this IEnumerable<T> varlist)
{ DataTable dtReturn = new DataTable(); // column names PropertyInfo[] oProps = null; // Could add a check to verify that there is an element 0 foreach (T rec in varlist)
{ // Use reflection to get property names, to create table, Only first time, others will follow if (oProps == null)
{ oProps = ((Type)rec.GetType()).GetProperties(); foreach (PropertyInfo pi in oProps)
{ Type colType = pi.PropertyType; if ((colType.IsGenericType) && (colType.GetGenericTypeDefinition() == typeof(Nullable<>)))
{ colType = colType.GetGenericArguments()[]; } dtReturn.Columns.Add(new DataColumn(pi.Name, colType)); } } DataRow dr = dtReturn.NewRow(); foreach (PropertyInfo pi in oProps)
{ dr[pi.Name] = pi.GetValue(rec, null) == null ? DBNull.Value : pi.GetValue(rec, null); } dtReturn.Rows.Add(dr); } return (dtReturn); }

Var 转 DataTable

Var To DataTable的更多相关文章

  1. DataTable to Excel(使用NPOI、EPPlus将数据表中的数据读取到excel格式内存中)

    /// <summary> /// DataTable to Excel(将数据表中的数据读取到excel格式内存中) /// </summary> /// <param ...

  2. jqurey datatable tableTools 自定义button元素 以及按钮自事件

    版本 1.10.4 "dom": 'T<"clear">lfrtip', "tableTools": { //"sSw ...

  3. datatable list 之前相互转换

    使用 FastMember: IEnumerable<SomeType> data = ... DataTable table = new DataTable(); using(var r ...

  4. DataTable AsEnumerable 的使用

    var p = DataTable.AsEnumerable().Where(t => t.Field<int>("ChannelID") == int.Pars ...

  5. webix .datatable 表格分页

    grid表格返回参数大都是 以下这种格式(参数名可能不一样) { data:[{...},{...} ...], count:39 } webix的参数格式为 { data:[{...},{...}, ...

  6. datatable动态列处理,重绘表格(敲黑板,划重点!!!我肝了一天半才彻底弄懂这个东西,TAT)

    datatable动态列处理,重绘表格 前言:至于动态列的绘画,我前面博客已经写过了,就是动态列的配置问题,不懂的去我博客看下,今天要写的呢,就是你已经写了一个动态列在datatable,现在你想重新 ...

  7. DataTable List 相互转换

    This uses the FastMember's meta-programming API for maximum performance. If you want to restrict it ...

  8. DataSet 和 DataTable 以及 DataRow

    向DataSet中添加DataTable 会提示datatable已属于另一个dataset 本来的想法是每次都new一个DataTable,但是还是会报错 百度了一下,发现可以调用DataTable ...

  9. jqurey datatable tableTools 自定义button元素 以及按钮定义事件

    版本 1.10.4 "dom": 'T<"clear">lfrtip', "tableTools": { //"sSw ...

随机推荐

  1. Swift中的Optional类型 (可选类型)与强制解包 ? !

    我们在swift的开发中会经常遇见?和! ,理解这两个符号深层次的内容对我们的开发是相当有利的: 目前网上对swift3.0的教程还相当的少,如果去搜索会发现早期的说法,在定义变量的时候,swift是 ...

  2. 如何对具有端点加密功能的LINE进行取证

    LINE又有新动作了,这回默认即启用了端点加密功能,强调确保传输过程的安全,且让我们来看看如何对付新版的LINE. 有启用Letter Sealing就会在昵称前多个锁头的图像. 这手机据犯嫌供称,落 ...

  3. windows 10启动盘制作工具

    Rufus 官方网站:http://rufus.akeo.ie/

  4. 云主机不能外网ssh连接,只能内网ssh连接的问题处理

    某台服务器外网无法ssh,内网可以ssh连接,ping值延时比较大 安装iftop查看流量 yum install -y iftop iftop界面含义如下 第一行:带宽显示 中间部分:外部连接列表, ...

  5. OpenSSL命令系列

    1.1 ssl命令系列前言 openssl命令的格式是"openssl command command-options args",command部分有很多种命令,这些命令需要依赖 ...

  6. linux进程及进程控制

    Linux进程控制   程序是一组可执行的静态指令集,而进程(process)是一个执行中的程序实例.利用分时技术,在Linux操作系统上同时可以运行多个进程.分时技术的基本原理是把CPU的运行时间划 ...

  7. 【转载】OpenGL超级宝典笔记——GLSL语言基础

    变量 GLSL的变量命名方式与C语言类似.变量的名称可以使用字母,数字以及下划线,但变量名不能以数字开头,还有变量名不能以gl_作为前缀,这个是GLSL保留的前缀,用于GLSL的内部变量.当然还有一些 ...

  8. C++ dll 通用dll编写

    头文件 extern "C" _declspec(dllexport)void AddFunction(); cpp文件 extern "C" _declspe ...

  9. MC java 远程调试 plugin 开发

    @ECHO OFF SET CATALINA_OPTS= -Xdebug -Xnoagent -Djava.compiler=NONE -Xrunjdwp:transport=dt_socket,se ...

  10. linux 实用知识整理

    http://www.apelearn.com/study_v2/ 查看端口占用 netstat -apn