• 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; }
        }
    View Code

    代码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};}}
    "); }
                            else
                            { sb.Append($"row["{propInfo.Name}"]=item.{propInfo.Name};
    "); }
                        }
                        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};}}
    "); }
                            else
                            { sb.Append($"if(item.{propInfo.Name}!=null){{row["{propInfo.Name}"]=item.{propInfo.Name};}}
    "); }
                        }
                    });
                    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};}}
    "); }
                    //        else
                    //        { sb.Append($"row["{propInfo.Name}"]=item.{propInfo.Name};
    "); }
                    //    }
                    //    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};}}
    "); }
                    //        else
                    //        { sb.Append($"if(item.{propInfo.Name}!=null){{row["{propInfo.Name}"]=item.{propInfo.Name};}}
    "); }
                    //    }
                    //});
                    //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;
            }
  • 相关阅读:
    SpringBoot框架(二)
    SpringBoot框架(一)
    JavaScript语言和jQuery技术(一)
    Mysql数据库技术(四)
    Mysql数据库技术(三)
    Mysql数据库技术(二)
    Mysql数据库技术(一)
    JDBC技术(三)
    JDBC技术(二)
    JDBC技术(一)
  • 原文地址:https://www.cnblogs.com/guxingy/p/10100888.html
Copyright © 2020-2023  润新知