C# 集合转换为DataTable
该类就用了几个类型,如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的更多相关文章
- linq之将IEnumerable<T>类型的集合转换为DataTable类型 (转载)
在考虑将表格数据导出成excel的时候,网上搜的时候找到一个特别合适的公共方法,可以将query查询出来的集合转换为datatable 需引用using System.Reflection; publ ...
- [工具类]泛型集合转换为DataTable
写在前面 在实际项目中,用到了将集合转换为DataTable,就试着封装了一个方法,记录一下. 代码 using System; using System.Collections.Generic; u ...
- 泛型集合转换为DataTable
在做项目中,遇到了将集合转换为DataTable的使用,在网上看了资料,在这里记录下来,分享. using System; using System.Collections.Generic; usin ...
- C#基础知识之泛型集合转换为DataTable
在做项目中,遇到了将集合转换为DataTable的使用,在网上看了资料,在这里记录下来,分享. using System; using System.Collections.Generic; usin ...
- c#将list集合转换为datatable的简单办法
public static class ExtensionMethods { /// <summary> /// 将List转换成DataTabl ...
- Json 字符串 转换为 DataTable数据集合
/// <summary> /// 将json转换为DataTable /// </summary> /// <param name="strJson" ...
- c#常用的Datable转换为json,以及json转换为DataTable操作方法
#region DataTable 转换为Json字符串实例方法 /// <summary> /// GetClassTypeJosn 的摘要说明 /// </summary> ...
- [Json] C#ConvertJson|List转成Json|对象|集合|DataSet|DataTable|DataReader转成Json (转载)
点击下载 ConvertJson.rar 本类实现了 C#ConvertJson|List转成Json|对象|集合|DataSet|DataTable|DataReader转成Json|等功能大家先预 ...
- 泛型集合、datatable常用数据类型转换Json帮助类
泛型集合.datatable常用数据类型转换Json帮助类 using System; using System.Data; using System.Configuration; using Sys ...
随机推荐
- python操作符与流程控制
操作符: 算术运算: + - * / % // ** 逻辑运算:and or not 身份运算: is not is 不可变数据类型:数字 字符串 字典key 可变数据 ...
- Spring之jdbcTemplate:查询的三种方式(单个值、单个对象、对象集合)
JdbcTemplateDemo2.java package helloworld.jdbcTemplate; import org.springframework.jdbc.core.JdbcTem ...
- 网易云音乐mp3外链、真实地址下载方法
一个网易音乐外链地址长期有效,很简单的方法: 第一步打开网易云音乐,随便找到一首歌,播放,复制网址的ID, 例如:杨钰莹的心雨,网址是: http://music.163.com/#/song?id= ...
- nginx, supervisor, celery
资料: supervisor和nginx使用 1 .supervisor 管理进程工具 2 .nginx 反向代理, 负载均衡 安装nginx $ sudo apt-get update $ su ...
- python中import和from-import的区别
. import导入模块的路径两种方式 # 将模块所在路径再程序中添加到sys.path列表中 import sys sys.path import导入模块按照sys.path顺序依次查找,“”代表当 ...
- <spark> error:启动spark后查看进程,进程中master和worker进程冲突
启动hadoop再启动spark后jps,发现master进程和worker进程同时存在,调试了半天配置文件. 测试发现,当我关闭hadoop后 worker进程还是存在, 但是,当我再关闭spark ...
- markdown的试用
因为markdown,我接触到latex,因为latex,我花了几个月去看相关的书籍 我看了以下相关的资料 1.<LaTeX入门> 刘海洋 2.英文 TeX - LaTeX Stack E ...
- Spring Cloud (6)config 客户端配置 与GitHub通信
1. 配置yml 1.1 1.2 1.3 2. 提交yml 到git 3.新建项目 pom 4.新建bootstrap.yml (优先权比application.yml高) 5.bootstrap.y ...
- element——message-box
`${action}`可以捕获用户选择cancel还是confirm,然后进行相应操作 官方文档:http://element-cn.eleme.io/#/zh-CN/component/messag ...
- HTML学习-1网页基础知识
HTML超文本标记语言:HyperText Markup Language. 由浏览器运行解析. 它包括了静态页面.html .htm.动态页面.php .aspx .jsp,从数据库提取. 今天 ...