该类就用了几个类型,如int,int?,string,所以其它类型就先没管。

用到的类:

    public class tb_Projects
{ public int ProID { get; set; }
public string ProjectName { get; set; }
/// <summary>
/// 编码
/// </summary>
public string ProjectCode { get; set; } public int ParentId { get; set; }
public int? NextId { get; set; }
public int? ProjectOrder { get; set; } public int IsEnabled { get; set; }
/// <summary>
/// 业主单位id
/// </summary>
public int? OwnerId { get; set; }
/// <summary>
/// 施工单位ID
/// </summary>
public int? ConstructionId { get; set; }
/// <summary>
/// 监理单位id
/// </summary>
public int? SupervisionId { get; set; }
/// <summary>
/// 承包单位id
/// </summary>
public int? ContractId { get; set; } /// <summary>
/// 第几级(即在树层次中第几级,根元素级次为1,以此类推)
/// </summary>
public int? Level { get; set; }
/// <summary>
/// 数量
/// </summary>
public int? Quantity { get; set; } public int VersionIng { get; set; } /// <summary>
/// 里程桩号
/// </summary>
public string MileageNo { get; set; }
/// <summary>
/// 标准编码
/// </summary>
public string ComponentCode { get; set; } /// <summary>
/// 内部编码
/// </summary>
public string NComponentCode { get; set; } /// <summary>
/// 流程状态
/// </summary>
public int TaskStatus { get; set; } public string FbxId { get; set; }
/// <summary>
/// 判断是否为单位工程
/// </summary>
public int IsSubunit { get; set; }
/// <summary>
/// 所属标段
/// </summary>
public string BiDSion { get; set; }
}

代码1:

StringBuilder sb = new StringBuilder();
Type elementType = typeof(Models.tb_Projects);
elementType.GetProperties().ToList().ForEach(propInfo =>
{
if (Nullable.GetUnderlyingType(propInfo.PropertyType) == null)
{
//普通类型
Type t = propInfo.PropertyType;
if (t == typeof(System.String))
{ sb.Append($"if(!string.IsNullOrWhiteSpace(item.{propInfo.Name})){{row[\"{propInfo.Name}\"]=item.{propInfo.Name};}}\r\n"); }
else
{ sb.Append($"row[\"{propInfo.Name}\"]=item.{propInfo.Name};\r\n"); }
}
else
{
//可为空类型
Type t = Nullable.GetUnderlyingType(propInfo.PropertyType);
if (t == typeof(System.String))
{ sb.Append($"if(!string.IsNullOrWhiteSpace(item.{propInfo.Name})){{row[\"{propInfo.Name}\"]=item.{propInfo.Name};}}\r\n"); }
else
{ sb.Append($"if(item.{propInfo.Name}!=null){{row[\"{propInfo.Name}\"]=item.{propInfo.Name};}}\r\n"); }
}
});
System.IO.File.WriteAllText("2.txt", sb.ToString());
Console.ReadLine();

代码2:

        public DataTable GetDataTable(List<tb_Projects> list)
{
DataTable dt = new DataTable(TableName);
Type elementType = typeof(tb_Projects);
elementType.GetProperties().ToList().ForEach(propInfo => dt.Columns.Add(propInfo.Name, Nullable.GetUnderlyingType(propInfo.PropertyType) ?? propInfo.PropertyType)); list = list.OrderBy(c => c.ProID).ToList();
list.ForEach(item =>
{
DataRow row = dt.NewRow(); #region 生成赋值语句 //StringBuilder sb = new StringBuilder();
//Type elementType = typeof(Models.tb_Projects);
//elementType.GetProperties().ToList().ForEach(propInfo =>
//{
// if (Nullable.GetUnderlyingType(propInfo.PropertyType) == null)
// {
// //普通类型
// Type t = propInfo.PropertyType;
// if (t == typeof(System.String))
// { sb.Append($"if(!string.IsNullOrWhiteSpace(item.{propInfo.Name})){{row[\"{propInfo.Name}\"]=item.{propInfo.Name};}}\r\n"); }
// else
// { sb.Append($"row[\"{propInfo.Name}\"]=item.{propInfo.Name};\r\n"); }
// }
// else
// {
// //可为空类型
// Type t = Nullable.GetUnderlyingType(propInfo.PropertyType);
// if (t == typeof(System.String))
// { sb.Append($"if(!string.IsNullOrWhiteSpace(item.{propInfo.Name})){{row[\"{propInfo.Name}\"]=item.{propInfo.Name};}}\r\n"); }
// else
// { sb.Append($"if(item.{propInfo.Name}!=null){{row[\"{propInfo.Name}\"]=item.{propInfo.Name};}}\r\n"); }
// }
//});
//System.IO.File.WriteAllText("2.txt", sb.ToString());
//Console.ReadLine(); //row["ProID"] = item.ProID;
//row["ProjectName"] = item.ProjectName;
//row["ParentId"] = item.ParentId;
//row["Level"] = item.Level;
//row["BiDSion"] = item.BiDSion;
//row["VersionIng"] = item.VersionIng;
//row["MileageNo"] = item.MileageNo;
//row["ProjectOrder"] = item.ProjectOrder;
//row["ProjectCode"] = item.ProjectCode;
//row["NComponentCode"] = item.NComponentCode;
//row["NEXTID"] = 0;
//row["ISENABLED"] = 0; #endregion //默认值 赋空
elementType.GetProperties().ToList().ForEach(propInfo => row[propInfo.Name] = DBNull.Value); //不为空 赋值
row["ProID"] = item.ProID;
if (!string.IsNullOrWhiteSpace(item.ProjectName)) { row["ProjectName"] = item.ProjectName; }
if (!string.IsNullOrWhiteSpace(item.ProjectCode)) { row["ProjectCode"] = item.ProjectCode; }
row["ParentId"] = item.ParentId;
if (item.NextId != null) { row["NextId"] = item.NextId; }
if (item.ProjectOrder != null) { row["ProjectOrder"] = item.ProjectOrder; }
row["IsEnabled"] = item.IsEnabled;
if (item.OwnerId != null) { row["OwnerId"] = item.OwnerId; }
if (item.ConstructionId != null) { row["ConstructionId"] = item.ConstructionId; }
if (item.SupervisionId != null) { row["SupervisionId"] = item.SupervisionId; }
if (item.ContractId != null) { row["ContractId"] = item.ContractId; }
if (item.Level != null) { row["Level"] = item.Level; }
if (item.Quantity != null) { row["Quantity"] = item.Quantity; }
row["VersionIng"] = item.VersionIng;
if (!string.IsNullOrWhiteSpace(item.MileageNo)) { row["MileageNo"] = item.MileageNo; }
if (!string.IsNullOrWhiteSpace(item.ComponentCode)) { row["ComponentCode"] = item.ComponentCode; }
if (!string.IsNullOrWhiteSpace(item.NComponentCode)) { row["NComponentCode"] = item.NComponentCode; }
row["TaskStatus"] = item.TaskStatus;
if (!string.IsNullOrWhiteSpace(item.FbxId)) { row["FbxId"] = item.FbxId; }
row["IsSubunit"] = item.IsSubunit;
if (!string.IsNullOrWhiteSpace(item.BiDSion)) { row["BiDSion"] = item.BiDSion; } //初值 仅这里使用
row["NextId"] = ;
row["IsEnabled"] = ; dt.Rows.Add(row);
}); return dt;
}

C# 集合转换为DataTable的更多相关文章

  1. linq之将IEnumerable<T>类型的集合转换为DataTable类型 (转载)

    在考虑将表格数据导出成excel的时候,网上搜的时候找到一个特别合适的公共方法,可以将query查询出来的集合转换为datatable 需引用using System.Reflection; publ ...

  2. [工具类]泛型集合转换为DataTable

    写在前面 在实际项目中,用到了将集合转换为DataTable,就试着封装了一个方法,记录一下. 代码 using System; using System.Collections.Generic; u ...

  3. 泛型集合转换为DataTable

    在做项目中,遇到了将集合转换为DataTable的使用,在网上看了资料,在这里记录下来,分享. using System; using System.Collections.Generic; usin ...

  4. C#基础知识之泛型集合转换为DataTable

    在做项目中,遇到了将集合转换为DataTable的使用,在网上看了资料,在这里记录下来,分享. using System; using System.Collections.Generic; usin ...

  5. c#将list集合转换为datatable的简单办法

    public static class ExtensionMethods        {        /// <summary>        /// 将List转换成DataTabl ...

  6. Json 字符串 转换为 DataTable数据集合

    /// <summary> /// 将json转换为DataTable /// </summary> /// <param name="strJson" ...

  7. c#常用的Datable转换为json,以及json转换为DataTable操作方法

    #region  DataTable 转换为Json字符串实例方法 /// <summary> /// GetClassTypeJosn 的摘要说明 /// </summary> ...

  8. [Json] C#ConvertJson|List转成Json|对象|集合|DataSet|DataTable|DataReader转成Json (转载)

    点击下载 ConvertJson.rar 本类实现了 C#ConvertJson|List转成Json|对象|集合|DataSet|DataTable|DataReader转成Json|等功能大家先预 ...

  9. 泛型集合、datatable常用数据类型转换Json帮助类

    泛型集合.datatable常用数据类型转换Json帮助类 using System; using System.Data; using System.Configuration; using Sys ...

随机推荐

  1. Java 7-Java 循环结构 - for, while 及 do…while

    Java 循环结构 - for, while 及 do...while 顺序结构的程序语句只能被执行一次.如果您想要同样的操作执行多次,,就需要使用循环结构. Java中有三种主要的循环结构: whi ...

  2. Windows Server 2012 NAT端口转发

     

  3. day1作业(格式化输出)

    练习:用户输入姓名.年龄.工作.爱好 ,然后打印成以下格式------------ info of Egon -----------Name  : EgonAge   : 22Sex   : male ...

  4. 动态参数(*args,**kwargs),命名空间和作用域,global和nonlocal,函数的嵌套

    1. 动态参数 位置参数的动态参数: *args 关键字参数的动态参数 : **kwargs 顺序: 位置,*args,默认值,**kwargs 在形参上*聚合, **聚合 在实参上*打散, **打散 ...

  5. Linux设置Oracle环境变量

    方法一:直接运行export命令定义变量,该变量只在当前的shell(BASH)或其子shell(BASH)下是有效的,shell关闭了,变量也就失效了,再打开新shell时就没有这个变量,需要使用的 ...

  6. asp mvc 导出txt 文件泛型方法

    asp mvc 导出txt 文件泛型方法分享: public static void ExportFile<T>(T obj) { StringBuilder str = new Stri ...

  7. JQ获取选中select 标签的值

    Jq://#ses为select 标签的Id$("#ses option:selected").val(); $("#ses option:selected") ...

  8. 【Selenium-WebDriver自学】WebDriver断言处理(十二)

    断言使用 http://www.cnblogs.com/itliucheng/p/5578788.html http://blog.csdn.net/gzh0222/article/details/7 ...

  9. tornado项目注意点

    大体框架思想 如果你做的项目是偏向中小型的话,MTV或者MVC已经足够支撑起整个项目,而如果你做的项目比较大大话,或者说可能以后的业务量很大的话,那你就需要用到四层架构的思想了,那么我们就各自分析下俩 ...

  10. hwclock

    查看.设定硬件时钟.该时钟由主机板的晶振及相关电路提供,需要主机板氧化银电池提供动力. 通过命令 hwclock 访问硬件时钟获取时间信息.该命令可以显示当前时间.重新设置时间.读取系统时间.设定系统 ...