public class List2DataTable     {

public static string GetClassName(Type type)

{             if (type == null)

throw new ArgumentException("参数type不能为空");

if (type.HasAttribute<AliasAttribute>())

return type.GetAttribute<AliasAttribute>().Name.ToLower();

else

return type.Name.ToLower();

}

public static DataTable ListToDataTable<T>(List<T> oList) where T : class

{

try

{

string tableName = GetClassName(typeof(T));

DataTable dt = new DataTable();

string fieldName;

object fieldValue;

string[] fieldNames;

System.Reflection.PropertyInfo[] Props = typeof(T).GetProperties();

fieldNames = new string[Props.Length];

for (int i = 0; i < Props.Length; i++)

{

fieldName = Props[i].Name;

fieldNames[i] = fieldName;

dt.Columns.Add(fieldName, Props[i].PropertyType);

}

for (int r = 0; r < oList.Count; r++)

{

DataRow dr = dt.NewRow();

T o = oList[r];

for (int i = 0; i < fieldNames.Length; i++)

{

fieldValue = Props[i].GetValue(o, null);

dr[fieldNames[i]] = fieldValue;

}

dt.Rows.Add(dr);

}

return dt;

}

catch(Exception ex)             {             }

return null;

}

}

List<T> 转换 DataTable的更多相关文章

  1. dataGrid转换dataTable

    #region dataGrid转换dataTable   /// <summary>   /// dataGrid转换dataTable   /// </summary>   ...

  2. C# 将Excel以文件流转换DataTable

    /* *引用 NuGet包 Spire.XLS */ /// <summary> /// Excel帮助类 /// </summary> public class ExcelH ...

  3. List转换DataTable

    /// <summary> /// 将泛类型集合List类转换成DataTable /// </summary> /// <param name="list&q ...

  4. DataRow数组转换DataTable

    public DataTable ToDataTable(DataRow[] rows) { if (rows == null || rows.Length == 0) return null; Da ...

  5. C# DataRow[]转换DataTable

    DataTable dt = ... DataRow[] dr = dt.Select("ID=14"); dt = dr.CopyToDataTable();

  6. c# 将csv文件转换datatable的两种方式。

    第一种: public static DataTable csvdatatable(string path) { DataTable dt = new DataTable(); string conn ...

  7. 实体lis<T>t转换datatable

    public static DataTable ListToTable<T>(List<T> list) {             Type type = typeof(T) ...

  8. 读CSV转换datatable

    using System.Data; using System.IO;   /// <summary> /// Stream读取.csv文件 /// </summary> // ...

  9. SqL读取XML、解析XML、SqL将XML转换DataTable、SqL将XML转换表

    DECLARE @ItemMessage XML )) SET @ItemMessage=N' <ReceivablesInfos> <ReceivablesList> < ...

随机推荐

  1. C#套接字和windowsAPI套接字

    C#服务器端 第一步:用指定的端口号和服务器的ip建立一个EndPoint对像:第二步:建立一个Socket对像:第三步:用socket对像的Bind()方法绑定EndPoint:第四步:用socke ...

  2. SSH框架 sequence diagram

  3. -Dmaven.multiModuleProjectDirectory system propery is not set. Check $M2_HOME environment variable and mvn script match.

    一, eclipse中使用maven插件的时候,运行run as maven build的时候报错 -Dmaven.multiModuleProjectDirectory system propery ...

  4. C++成员变量的初始化顺序问题

    问题来源: 由于面试题中,考官出了一道简单的程序输出结果值的题:如下, class A { private: int n1; int n2; public: A():n2(0),n1(n2+2){} ...

  5. HTML5+AJAX原生分块上传文件的关键参数设置

    processData:false 这是jquery.ajax的一个参数.默认值为true,表示会将非字符串对象自动变成k1=v1&k2=v2的形式,例如一个数组参数{d:[1,2]},到服务 ...

  6. 网页之间信息传递方式(Cookie,Session)

    1.使用header()函数的重定向方式实现网页跳转.   EXE:header("Location: http://www.example.com/");   2.URL的GET ...

  7. cmd 下telnet 不是内部或外部命令

    问题:cmd 下telnet 提示不是内部或外部命令 解决方案:

  8. java-汉字转化拼音(纯java)

    1.转换所有的拼音 import java.util.Iterator; import java.util.LinkedHashMap; import java.util.Set; public cl ...

  9. ml的线性回归应用(python语言)

    线性回归的模型是:y=theta0*x+theta1   其中theta0,theta1是我们希望得到的系数和截距. 下面是代码实例: 1. 用自定义数据来看看格式: # -*- coding:utf ...

  10. [bzoj1787][Ahoi2008]紧急集合

    Description 给定一棵大小为的树,有组询问,每组询问给三个点,求到这三个点距离和最小的点及最小距离和. Input 第一行两个数. 接下来行,每行两个数表示到有一条边. 最后行,每行个数,为 ...