欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

C# 集合转换为DataTable

程序员文章站 2022-11-22 15:07:23
该类就用了几个类型,如int,int?,string,所以其它类型就先没管。用到的类:public class tb_Projects { public int ProID { get; set; } public string ProjectName { get; set; } /// /// 编码 /// ...

该类就用了几个类型,如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"] = 0;
                row["IsEnabled"] = 0;

                dt.Rows.Add(row);
            });

            return dt;
        }

本文地址:https://blog.csdn.net/weixin_40362806/article/details/108992695

相关标签: C#编程语言