zoukankan      html  css  js  c++  java
  • 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;
            }

  • 相关阅读:
    CString与char *互转总结
    string 与char* char[]之间的转换
    VC++下使用SQLite数据库
    VC连接数据库方式
    C/C++中判断某一文件或目录是否存在
    漂亮的CSS按钮样式集以及在线生成工具
    PhpStorm 4.0 & 5.0 部署本地Web应用 (转)
    PHP的serialize序列化数据与JSON格式化数据
    c/c++中产生随机数
    [STL系列]开篇简单介绍
  • 原文地址:https://www.cnblogs.com/zhouyunbaosujina/p/3297561.html
Copyright © 2011-2022 走看看