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

  • 相关阅读:
    ActiveX控件的注册和反注册
    谷歌浏览器调用activex控件方法
    VC 中的ATL ActiveX 和 MFC ActiveX 有什么区别
    DirectX介绍(转)
    最简单的基于FFMPEG的图像编码器(YUV编码为JPEG)(转)
    统计Visual Studio项目的代码行数
    【Sqlite3】sqlite_sequence表(转)
    linuxunix系统下的字符操作
    tif
    字符串截取
  • 原文地址:https://www.cnblogs.com/zhouyunbaosujina/p/3297561.html
Copyright © 2020-2023  润新知