该类就用了几个类型,如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. vue 要点

    一: 1.  如果在实例创建之后添加新的属性到实例上,它不会触发视图更新. 2.  v-show 的元素会始终渲染并保持在 DOM 中.v-show 是简单的切换元素的 CSS 属性 display.

  2. tp3.2分页

    tp3.2分页 使用tp3.2自带的分页类 <?php // +----------------------------------------------------------------- ...

  3. pycharm中导入requests,xmlx等模块的方法。

    现在pycharm的功能越来越强大,我们需要什么直接就可以导入: 下面正式开始介绍: 第一步: 先选择左边的:project interpreter   ----->再点击后面的绿色的加号. 那 ...

  4. 数据库数据以Excel的方式导出

    import java.io.Serializable; import java.util.List; import com.cfets.cwap.s.util.db.TableColumn; /** ...

  5. SQL server 数据库的数据完整性

    存储在数据库中的所有数据值均正确的状态.如果数据库中存储有不正确的数据值,则该数据库称为已丧失数据完整性. 详细释义 数据库中的数据是从外界输入的,而数据的输入由于种种原因,会发生输入无效或 错误信息 ...

  6. 性能测试day01_性能基本概念

    其实第一次接触性能是15年的时候,懵懵懂懂的被领导拉去做第一次做性能压测,如今有机会重新听一下云层大大讲解性能,于是打算以此博客记录下整个学习的过程,如若有不同意见者可以在下面留言指出,也欢迎大家一起 ...

  7. RPM软件管理工具

    1 概述 RPM(RedHat Package Manager),Rethat软件包管理工具,类似windows里面的setup.exe 是Linux这系列操作系统里面的打包安装工具,它虽然是RedH ...

  8. error C2011: “timespec”:“struct”类型重定义

    error C2011: “timespec”:“struct”类型重定义 C++ pthread pthread.h 中的 timespec 和time.h 中的 结构定义重复了 ,同时两个头文件中 ...

  9. C++ Sleep() sleep()

    简介: 函数名: sleep 功 能: 执行挂起一段时间 用 法: unsigned sleep(unsigned seconds); 在VC中使用带上头文件 #include <windows ...

  10. python venv actieve uninstall pack-name sitepage