• 首页 首页 icon
  • 工具库 工具库 icon
    • IP查询 IP查询 icon
  • 内容库 内容库 icon
    • 快讯库 快讯库 icon
    • 精品库 精品库 icon
    • 问答库 问答库 icon
  • 更多 更多 icon
    • 服务条款 服务条款 icon

DataTable转换为List<T>以和<T>类

武飞扬头像
Pass_Time_
帮助1

有时候我们需要将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
        }
}
学新通

这篇好文章是转载于:学新通技术网

  • 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
  • 本站站名: 学新通技术网
  • 本文地址: /boutique/detail/tanhfiigia
系列文章
更多 icon
同类精品
更多 icon
继续加载