该类就用了几个类型,如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. 18 LVM逻辑卷管理

    根据上一节的内容,我们知道md这个内核模块可以用来做软RAID的管理.同时RAID实现了两个功能:1.提高了磁盘的读写能力:2.对于数据进行了冗余备份: 但是,如果是管理员手动误删的数据,则一样无法找 ...

  2. Masonry基本语法

    添加约束的方式: 1.通过使用NSLayoutConstraints添加约束到约束数组中,之前必须设置translatesAutoresizingMaskIntoConstraints = NO,即取 ...

  3. Hive与HBase集成进行数据分析

    我们把hive的安装包上传的节点3来 解压 现在我们还是老规矩通过notopad++来连接我们的虚拟机来配置文件,把下面这两个文件重命名一下 修改这个文件 对hive-env.sh我们修改这里 下面我 ...

  4. Java 9 - Java Number类

    Java Number类 一般地,当需要使用数字的时候,我们通常使用内置数据类型,如:byte.int.long.double等. 实例 int i = 5000; float gpa = 13.65 ...

  5. string类和stringBuilder类

    字符串是C#中的一种重要数据类型,在项目开发中,离不开字符串操作.C#提供了string类实现字符串操作.于Convert类相似,string类中方法有静态方法和非静态方法.注意,在C#中String ...

  6. [转]asp+oracle分页

    PageSize:每页显示的记录数.PageCount:根据用户设定好的PageSize和表中的总记录数,系统自动算出总页数.RecordCount:表中的总记录数.AbsolutePage:表示当前 ...

  7. redis下操作String

    redis操作string string是redis最基本的类型 最大能存储512MB数据 string类型是二进制安全的,即可以为任何数据,比如数字.图片.序列化对象等 基本命令 设置 设置键值 s ...

  8. python二进制转换

    例一.题目描述: 输入一个整数,输出该数二进制表示中1的个数.其中负数用补码表示. 分析: python没有unsigned int类型 >>> print ("%x&qu ...

  9. 解决Cell重用问题

    在显示的过程中,出现了内容重叠的问题,其实就是UITableViewCell重用机制的问题. 解决方法一:对在cell中添加的控件设置tag的方法 在cell的contentView上需要添加控件,那 ...

  10. 搭建eclipse开发环境

    eclipse-jee配置 基本配置: 快捷查找:window->perferences->搜索框搜索 utf8: window->perferences->general-& ...