该类就用了几个类型,如int,int?,string,所以其它类型就先没管。
用到的类:
public class tb_Projects { public int ProID { get; set; } public string ProjectName { get; set; } ////// 编码 /// 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; } ////// 业主单位id /// public int? OwnerId { get; set; } ////// 施工单位ID /// public int? ConstructionId { get; set; } ////// 监理单位id /// public int? SupervisionId { get; set; } ////// 承包单位id /// public int? ContractId { get; set; } ////// 第几级(即在树层次中第几级,根元素级次为1,以此类推) /// public int? Level { get; set; } ////// 数量 /// public int? Quantity { get; set; } public int VersionIng { get; set; } ////// 里程桩号 /// public string MileageNo { get; set; } ////// 标准编码 /// public string ComponentCode { get; set; } ////// 内部编码 /// public string NComponentCode { get; set; } ////// 流程状态 /// public int TaskStatus { get; set; } public string FbxId { get; set; } ////// 判断是否为单位工程 /// public int IsSubunit { get; set; } ////// 所属标段 /// 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(Listlist) { 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"] = 0; row["IsEnabled"] = 0; dt.Rows.Add(row); }); return dt; }