将DataTable转换为List<T>以及<T>类

有时候我们需要将sql查询返回的DataTable转换为类。最开始是使用循环一个个给类的属性赋值,但是这样效率低并且无法复用。

后来了解到利用DataTable添加扩展方法可以轻松的实现这一功能

  1. 将DataTable转化为List,使用DataTable.ToList();

  2. 将DataTable转化为,使用DataTable.ToData();

  3. 填充已有内容,使用DataTable.FillData();

注:

  1. ConvertHelper类请参考 https://www.cnblogs.com/axiaoshuye/articles/15697734.html

  2. 转换的前提条件是DataTable字段名要与对应类的属性名一致

using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DataTableEx
{
    
    
    public static class DataTableEx
    {
    
    
        public static List<T> ToList<T>(this DataTable dt) where T : new()
        {
    
    
            List<T> ts = new List<T>();
            foreach (DataRow dr in dt.Rows)
            {
    
    
                T t = new T();
                foreach (var c in dt.Columns)
                {
    
    
                    object value = dr[c.ToString()];
                    if (value != DBNull.Value)
                    {
    
    
                        var p = t.GetType().GetProperty(c.ToString());
                        if (p != null)
                        {
    
    
                            p.SetValue(t, ConvertHelper.ChangeType(value, p.PropertyType), null);
                        }
                    }
                }
                ts.Add(t);
            }
            return ts;
        }


        public static T ToData<T>(this DataTable dt) where T : new()
        {
    
    
            if (dt.Rows.Count>1)
            {
    
    
                throw new Exception("");
            }
            List<T> ts = new List<T>();
            foreach (DataRow dr in dt.Rows)
            {
    
    
                T t = new T();
                foreach (var c in dt.Columns)
                {
    
    
                    object value = dr[c.ToString()];
                    if (value != DBNull.Value)
                    {
    
    
                        var p = t.GetType().GetProperty(c.ToString());
                        if (p != null)
                        {
    
    
                            p.SetValue(t, ConvertHelper.ChangeType(value, p.PropertyType), null);
                        }
                    }
                }
                return t;
            }
            return default(T);
        }

        public static void  FillData<T>(this DataTable dt,ref T t) where T : new()
        {
    
    
            if (dt.Rows.Count > 1)
            {
    
    
                throw new Exception("");
            }
            foreach (DataRow dr in dt.Rows)
            {
    
    
        
                foreach (var c in dt.Columns)
                {
    
    
                    object value = dr[c.ToString()];
                    if (value != DBNull.Value)
                    {
    
    
                        var p = t.GetType().GetProperty(c.ToString());
                        if (p != null)
                        {
    
    
                            p.SetValue(t, ConvertHelper.ChangeType(value, p.PropertyType), null);
                        }
                    }
                }
            }
        }

    }
    public static class ConvertHelper
        {
    
    
            #region = ChangeType =
            public static object ChangeType(object obj, Type conversionType)
            {
    
    
                return ChangeType(obj, conversionType, System.Threading.Thread.CurrentThread.CurrentCulture);
            }

            public static object ChangeType(object obj, Type conversionType, IFormatProvider provider)
            {
    
    

                #region Nullable
                Type nullableType = Nullable.GetUnderlyingType(conversionType);
                if (nullableType != null)
                {
    
    
                    if (obj == null)
                    {
    
    
                        return null;
                    }
                    return Convert.ChangeType(obj, nullableType, provider);
                }
                #endregion
                if (typeof(System.Enum).IsAssignableFrom(conversionType))
                {
    
    
                    return Enum.Parse(conversionType, obj.ToString());
                }
                return Convert.ChangeType(obj, conversionType, provider);
            }
            #endregion
        }
}

猜你喜欢

转载自blog.csdn.net/weixin_45499836/article/details/126567652