该类就用了几个类型,如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. ctags使用

    1:安装ctags sudo apt-get install exuberant-ctags ctags --help 2:建立源码之间的组织关系: 1:ctags ./*.c -R 生成tags文件 ...

  2. gulp 配置达到实现import export支持

    gulp.task('tojs', () => { return gulp.src('./es/**/*.js') .pipe(babel({ babelrc: false, plugins: ...

  3. mobx.js 使用教程-react

    1.store: import { observer } from "mobx-react"; import { observable, action, computed ,aut ...

  4. How Computers Boot Up.计算机的引导过程

    原文标题:How Computers Boot Up 原文地址:http://duartes.org/gustavo/blog/ [注:本人水平有限,只好挑一些国外高手的精彩文章翻译一下.一来自己复习 ...

  5. Android被忽略的tools

    自动生成的布局xml文件,很多都带有tools字样:但是大部分都被我们删除了: 其实它的作用是让我们这些开发者预览用的,十分的方便: 事例一个TextView: <TextView androi ...

  6. NoSQL学习1

    MongoDB使用C++语言编写的一个基于分布式的文件存储的开源数据库.可以在承受高负载的情况下,保证服务器的性能. MongoDB将数据存储成为一个文档,数据结构有键值对组成.类似于JSON,字段值 ...

  7. 微信小程序连接本地接口(转)

    原文地址 最近的一个项目就是微信小程序 第一次接触微信开发者工具,并进行小程序的后端开发, 于是想看一下小程序如何请求本地的后台服务接口 wx.request({ url: 'http://local ...

  8. tornado 和 djanjo 转义处理对比

    tornado tornado默认是转义所有字符,比较安全,但有时候我们的确需要把字符当做html来解析处理,因此我们需要做些处理. 所有的模板输出都已经通过 tornado.escape.xhtml ...

  9. android 开发 写一个RecyclerView布局的聊天室,并且添加RecyclerView的点击事件

    实现思维顺序: 1.首先我们需要准备2张.9的png图片(一张图片为左边聊天泡泡,一个图片为右边的聊天泡泡),可以使用draw9patch.bat工具制作,任何图片导入到drawable中. 2.需要 ...

  10. Error: 实例 "ddd" 执行所请求操作失败,实例处于错误状态。: 请稍后再试 [错误: Exceeded maximum number of retries. Exhausted all hosts available for retrying build failures for instance 6f60bc06-fcb6-4758-a46f-22120ca35a71.].

    Error: 实例 "ddd" 执行所请求操作失败,实例处于错误状态.: 请稍后再试 [错误: Exceeded maximum number of retries. Exhaus ...