Var To DataTable
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的更多相关文章
- DataTable to Excel(使用NPOI、EPPlus将数据表中的数据读取到excel格式内存中)
/// <summary> /// DataTable to Excel(将数据表中的数据读取到excel格式内存中) /// </summary> /// <param ...
- jqurey datatable tableTools 自定义button元素 以及按钮自事件
版本 1.10.4 "dom": 'T<"clear">lfrtip', "tableTools": { //"sSw ...
- datatable list 之前相互转换
使用 FastMember: IEnumerable<SomeType> data = ... DataTable table = new DataTable(); using(var r ...
- DataTable AsEnumerable 的使用
var p = DataTable.AsEnumerable().Where(t => t.Field<int>("ChannelID") == int.Pars ...
- webix .datatable 表格分页
grid表格返回参数大都是 以下这种格式(参数名可能不一样) { data:[{...},{...} ...], count:39 } webix的参数格式为 { data:[{...},{...}, ...
- datatable动态列处理,重绘表格(敲黑板,划重点!!!我肝了一天半才彻底弄懂这个东西,TAT)
datatable动态列处理,重绘表格 前言:至于动态列的绘画,我前面博客已经写过了,就是动态列的配置问题,不懂的去我博客看下,今天要写的呢,就是你已经写了一个动态列在datatable,现在你想重新 ...
- DataTable List 相互转换
This uses the FastMember's meta-programming API for maximum performance. If you want to restrict it ...
- DataSet 和 DataTable 以及 DataRow
向DataSet中添加DataTable 会提示datatable已属于另一个dataset 本来的想法是每次都new一个DataTable,但是还是会报错 百度了一下,发现可以调用DataTable ...
- jqurey datatable tableTools 自定义button元素 以及按钮定义事件
版本 1.10.4 "dom": 'T<"clear">lfrtip', "tableTools": { //"sSw ...
随机推荐
- [Appium] 使用Appium过程中遇到的各种坑
以下问题都是以ios为背景: 1. 问题: Case: 在页面S1上,点击元素A后,判读B元素是否出现. Detail:一开始通过Appium Inspector, 可以找到B元素,所以直接取该元素的 ...
- [原创] ubuntu下安装scrapy报错 error: command 'x86_64-linux-gnu-gcc' failed with exit status 1
Ubuntu14.04在virtualenv下安装scrapy报错,Failed building wheel for cffi,lxml,cryptography 等. error: command ...
- Python isdigit()方法
描述 Python isdigit() 方法检测字符串是否只由数字组成. 语法 isdigit()方法语法: str.isdigit() 参数 无. 返回值 如果字符串只包含数字则返回 True 否则 ...
- The first day!!!
我的小院子开通啦,开始记录自己的学习历程,加油!!!
- css绘制特殊图形基础
1.等腰三角形 .isosceles{ width:; height:; border:30px solid; border-left-color: transparent; border-right ...
- js onkeypress与onkeydown 事件区别详细说明
keypress只适用于有字符输入的按键 而keyup/keydown包含了Ctrl, Shift之类的情况 Firefox在处理onKeyDown/onKeyPress事件时存在漏洞,恶意网页可能利 ...
- GBDT(MART) 迭代决策树入门教程 | 简介
GBDT(MART) 迭代决策树入门教程 | 简介 http://blog.csdn.net/w28971023/article/details/8240756
- js实现倒计时及时间对象
JS实现倒计时效果代码如下: <!doctype html> <html> <head> <meta charset="utf-8"> ...
- xcode archive 一直是灰色的
把配置选择为device才能选build and archive的,模拟器的肯定不能build and anchive
- CoreData创建
云盘-->CoreData文件 一 创建表打开点击增加的Entity名字应该与 EntityForName一致 二 创建NSManageObject SubClass 三 拖进CoreD ...