DataTable转换成实体列表 与 实体列表转换成DataTable

/// <summary>
        /// DataTable转换成实体列表
        /// </summary>
        /// <typeparam name="T">实体 T </typeparam>
        /// <param name="table">datatable</param>
        /// <returns></returns>
        public static IList<T> DataTableToList<T>(DataTable table)
            where T : class
        {
            if (!IsHaveRows(table))
                return new List<T>();

            IList<T> list = new List<T>();
            T model = default(T);
            foreach (DataRow dr in table.Rows)
            {
                model = Activator.CreateInstance<T>();

                foreach (DataColumn dc in dr.Table.Columns)
                {
                    object drValue = dr[dc.ColumnName];
                    PropertyInfo pi = model.GetType().GetProperty(dc.ColumnName.Trim());

                if (pi != null && pi.CanWrite && drValue != null && !Convert.IsDBNull(drValue)&&!string.IsNullOrEmpty(drValue.ToString()))
                {
                    if (pi.PropertyType == typeof(Guid))
                    {
                        pi.SetValue(model, Guid.Parse(drValue.ToString()), null);
                    }
                    else if (pi.PropertyType == typeof(Guid?))
                    {
                        pi.SetValue(model, Guid.Parse(drValue.ToString()), null);
                    }
                    else if (pi.PropertyType == typeof(NonWasteCellPointLevel)||pi.PropertyType == typeof(DeclareStatusEnum))
                    {
                        pi.SetValue(model, Enum.Parse(pi.PropertyType,drValue.ToString()), null);
                    }
                    else if (pi.PropertyType == typeof(bool)&&(drValue.ToString()!="true"||drValue.ToString()!="false"))
                    {
                        var value = int.Parse(drValue.ToString());
                        
                        if ( value== 1)
                        {
                            pi.SetValue(model, true, null);
                        }
                        else
                        {
                            pi.SetValue(model, false, null);
                        }
                    }
                    else
                    {
                        object convertedValue = Convert.ChangeType(drValue, pi.PropertyType);
                        pi.SetValue(model, convertedValue, null);
                    }
                }
                }

                list.Add(model);
            }
            return list;
        }

 /// <summary>
        /// 实体列表转换成DataTable
        /// </summary>
        /// <typeparam name="T">实体</typeparam>
        /// <param name="list"> 实体列表</param>
        /// <returns></returns>
        public static DataTable ListToDataTable<T>(IList<T> list)
            where T : class
        {
            if (list == null || list.Count <= 0)
            {
                return null;
            }
            DataTable dt = new DataTable(typeof(T).Name);
            DataColumn column;
            DataRow row;

            PropertyInfo[] myPropertyInfo = typeof(T).GetProperties(BindingFlags.Public | BindingFlags.Instance);

            bool createColumn = true;

            foreach (T t in list)
            {
                if (t == null)
                {
                    continue;
                }

                row = dt.NewRow();
                for (int i = 0; i < myPropertyInfo.Length; i++)
                {
                    PropertyInfo pi = myPropertyInfo[i];
                    if (createColumn)
                    {
                        column = new DataColumn(pi.Name,pi.PropertyType );
                        dt.Columns.Add(column);
                    }
                    row[pi.Name] = pi.GetValue(t, null);
                }

                if (createColumn)
                {
                    createColumn = false;
                }

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

        }

如果提示 没有类型System.Nullable`1 错误,说明你在实体中定义字段时加上了?(可为空),比如 int? id ,此时就会报这个错。 

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

雨中深巷的油纸伞

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值