zoukankan      html  css  js  c++  java
  • 实体类和DataTable的转换

    引子

    最近在项目中在数据库查询的时间,总是要用到数据表到实体类对象列表的转化,自己封装了一个转换的方法,用起来还比较方便,记下来,以后可以重复使用,原理就主要是利用反射,当然有更好的ORM框架可以实现,主要的原因就是我这里没有用orm。

    实现

    话不多少,直接上实现代码

     /// <summary>
        /// 数据表转换类
        /// </summary>
        /// <typeparam name="T"></typeparam>
        public class DbTableConvertor<T> where T : new()
        {
            /// <summary>
            /// 将DataTable转换为实体列表
            /// 作者: oldman
            /// 创建时间: 2015年9月13日
            /// </summary>
            /// <param name="dt">待转换的DataTable</param>
            /// <returns></returns>
            public List<T> ConvertToList(DataTable dt)
            {
                // 定义集合  
                var list = new List<T>();
    
                if (0 == dt.Rows.Count)
                {
                    return list;
                }
    
                //遍历DataTable中所有的数据行  
                foreach (DataRow dr in dt.Rows)
                {
                    var entity = new T();
    
                    // 获得此模型的公共属性  
                    var propertys = entity.GetType().GetProperties();
    
                    //遍历该对象的所有属性  
                    foreach (var p in propertys)
                    {
                        //将属性名称赋值给临时变量
                        string tmpName = p.Name;
    
                        //检查DataTable是否包含此列(列名==对象的属性名)    
                        if (dt.Columns.Contains(tmpName))
                        {
                            // 判断此属性是否有Setter
                            if (!p.CanWrite)
                            {
                                continue; //该属性不可写,直接跳出
                            }
    
                            //取值  
                            var value = dr[tmpName];
    
                            //如果非空,则赋给对象的属性  
                            if (value != DBNull.Value)
                            {
                                p.SetValue(entity, value, null);
                            }
                        }
                    }
                    //对象添加到泛型集合中  
                    list.Add(entity);
                }
    
                return list;
            }
    
            /// <summary>
            /// 将DataTable的首行转换为实体
            /// 作者: oldman
            /// 创建时间: 2015年9月13日
            /// </summary>
            /// <param name="dt">待转换的DataTable</param>
            /// <returns></returns>
            public T ConvertToEntity(DataTable dt)
            {
                var entity = new T();
    
                if (0 == dt.Rows.Count)
                {
                    return entity;
                }
    
                // 获得此模型的公共属性  
                var propertys = entity.GetType().GetProperties();
    
                //遍历该对象的所有属性  
                foreach (var p in propertys)
                {
                    //将属性名称赋值给临时变量
                    string tmpName = p.Name;
    
                    //检查DataTable是否包含此列(列名==对象的属性名)    
                    if (dt.Columns.Contains(tmpName))
                    {
                        // 判断此属性是否有Setter
                        if (!p.CanWrite)
                        {
                            continue; //该属性不可写,直接跳出
                        }
    
                        //取值  
                        var value = dt.Rows[0][tmpName];
    
                        //如果非空,则赋给对象的属性  
                        if (value != DBNull.Value)
                        {
                            p.SetValue(entity, value, null);
                        }
                    }
                }
    
                return entity;
            }
        }

    更改

    在几位院子里大神的指点下,做了一些修改,以后应该多注意这些细节,感谢@双鱼座和@梦在旅途,修改后的如下

    using System;
    using System.Collections.Generic;
    using System.Data;
    using System.Linq;
    using System.Reflection;
    
    namespace SolrNetDemo
    {
        /// <summary>
        ///     数据表转换类
        /// </summary>
        /// <typeparam name="T"></typeparam>
        public class DbTableConvertor<T> where T : new()
        {
            /// <summary>
            ///     将DataTable转换为实体列表
            ///     作者: oldman
            ///     创建时间: 2015年9月13日
            /// </summary>
            /// <param name="dt">待转换的DataTable</param>
            /// <returns></returns>
            public List<T> ConvertToList(DataTable dt)
            {
                // 定义集合  
                var list = new List<T>();
    
                if (0 == dt.Rows.Count)
                {
                    return list;
                }
    
                // 获得此模型的可写公共属性  
                IEnumerable<PropertyInfo> propertys = new T().GetType().GetProperties().Where(u => u.CanWrite);
                list = ConvertToEntity(dt, propertys);
    
    
                return list;
            }
    
            /// <summary>
            ///     将DataTable的首行转换为实体
            ///     作者: oldman
            ///     创建时间: 2015年9月13日
            /// </summary>
            /// <param name="dt">待转换的DataTable</param>
            /// <returns></returns>
            public T ConvertToEntity(DataTable dt)
            {
                DataTable dtTable = dt.Clone();
                dtTable.Rows.Add(dt.Rows[0].ItemArray);
                return ConvertToList(dtTable)[0];
            }
    
    
            private List<T> ConvertToEntity(DataTable dt, IEnumerable<PropertyInfo> propertys)
            {
                var list = new List<T>();
                //遍历DataTable中所有的数据行  
                foreach (DataRow dr in dt.Rows)
                {
                    var entity = new T();
    
                    //遍历该对象的所有属性  
                    foreach (PropertyInfo p in propertys)
                    {
                        //将属性名称赋值给临时变量
                        string tmpName = p.Name;
    
                        //检查DataTable是否包含此列(列名==对象的属性名)    
                        if (!dt.Columns.Contains(tmpName)) continue;
                        //取值  
                        object value = dr[tmpName];
                        //如果非空,则赋给对象的属性  
                        if (value != DBNull.Value)
                        {
                            p.SetValue(entity, value, null);
                        }
                    }
                    //对象添加到泛型集合中  
                    list.Add(entity);
                }
                return list;
            }
        }
    }

    确实看清了进步的空间,希望大家多交流,共同进步。

    注释的都很清楚,欢迎拍砖。

    本人的.NET学习技术交流群:226704167

    努力学习,只为一个梦想。

  • 相关阅读:
    URAL 1998 The old Padawan 二分
    URAL 1997 Those are not the droids you're looking for 二分图最大匹配
    URAL 1995 Illegal spices 贪心构造
    URAL 1993 This cheeseburger you don't need 模拟题
    URAL 1992 CVS
    URAL 1991 The battle near the swamp 水题
    Codeforces Beta Round #92 (Div. 1 Only) A. Prime Permutation 暴力
    Codeforces Beta Round #7 D. Palindrome Degree hash
    Codeforces Beta Round #7 C. Line Exgcd
    Codeforces Beta Round #7 B. Memory Manager 模拟题
  • 原文地址:https://www.cnblogs.com/lip0121/p/4804049.html
Copyright © 2011-2022 走看看