• C# DataTable转实体 通用方法【转】


    public static T GetEntity<T>(DataTable table) where T : new()
        {
            T entity = new T();
            foreach (DataRow row in table.Rows)
            {
                foreach (var item in entity.GetType().GetProperties())
                {
                    if (row.Table.Columns.Contains(item.Name))
                    {
                        if (DBNull.Value != row[item.Name])
                        {
                            item.SetValue(entity, Convert.ChangeType(row[item.Name], item.PropertyType), null);
                        }

                    }
                }
            }

            return entity;
        }

    dataTable转化为实体列表MethodOne:

        public static IList<T> GetEntities<T>(DataTable table) where T : new()
        {
            IList<T> entities = new List<T>();
            foreach (DataRow row in table.Rows)
            {
                T entity = new T();
                foreach (var item in entity.GetType().GetProperties())
                {
                    item.SetValue(entity, Convert.ChangeType(row[item.Name], item.PropertyType), null);
                }
                entities.Add(entity);
            }
            return entities;
        }

    MethodTwo:

     /// <summary>
            /// DataTable 转化为对象集合
            /// </summary>
            /// <typeparam name="TEntity"></typeparam>
            /// <param name="dt"></param>
            /// <returns></returns>
            public static List<TEntity> ToList<TEntity>(DataTable dt) where TEntity : new()
            {
                List<TEntity> listEntity = new List<TEntity>();
                if (dt != null && dt.Rows.Count > 0)
                {
                    int fieldCount = dt.Columns.Count;
                    foreach (DataRow item in dt.Rows)
                    {
                        TEntity t = (TEntity)Activator.CreateInstance(typeof(TEntity));

                        for (int i = 0; i < fieldCount; i++)
                        {
                            PropertyInfo field = t.GetType().GetProperty(dt.Columns[i].ColumnName, BindingFlags.Public | BindingFlags.Instance | BindingFlags.IgnoreCase);
                            if (field != null)
                            {
                                if (item[i] == null || Convert.IsDBNull(item[i]))
                                {
                                    field.SetValue(t, null, null);
                                }
                                else
                                {
                                    field.SetValue(t, item[i], null);
                                }
                            }
                        }

                        listEntity.Add(t);
                    }
                }
                if (listEntity.Count == 0)
                {
                    return null;
                }

                return listEntity;
            }

  • 相关阅读:
    javaSE一些实习问题
    java并行程序基础
    http与https协议
    mybatis-plus 狂神说笔记
    弄懂java bio 和 nio 一篇就够 !!!
    异步操作Promise
    uni-app,vue-cli3或4的跨域
    7.vue之v-on
    Linux Shell 错误: $' ': command not found错误解决
    visual studio 容器工具首次加载太慢 vsdbgvs2017u5 exists, deleting 的解决方案
  • 原文地址:https://www.cnblogs.com/zhouyunbaosujina/p/3297561.html
Copyright © 2020-2023  润新知