zoukankan      html  css  js  c++  java
  • 将DataTable转换成类的方法

    /// <summary>

        /// 使用类的属性名对应DataTable中的字段名

        /// </summary>

        public static class TableToModel

        {

            /// <summary>

            /// DataRow扩展方法:将DataRow类型转化为指定类型的实体

            /// </summary>

            /// <typeparam name="T">实体类型</typeparam>

            /// <returns></returns>

            public static T ToModel<T>(this DataRow dr) where T : class, new()

            {

                return ToModel<T>(dr, true);

            }

            /// <summary>

            /// 将DataRow类型转化为指定类型的实体

            /// </summary>

            /// <typeparam name="T">实体类型</typeparam>

            /// <param name="dateTimeToString">是否需要将日期转换为字符串,默认为转换,值为true</param>

            /// <returns></returns>

            /// <summary>

            public static T ToModel<T>(this DataRow dr, bool dateTimeToString) where T : class, new()

            {

                if (dr != null)

                    return ToList<T>(dr.Table, dateTimeToString).First();

                return null;

            }

            /// <summary>

            /// 将DataTable类型转化为指定类型的实体集合

            /// </summary>

            /// <typeparam name="T">实体类型</typeparam>

            /// <returns></returns>

            public static List<T> ToList<T>(this DataTable dt) where T : class, new()

            {

                return ToList<T>(dt, true);

            }

            /// <summary>

            ///将DataTable类型转化为指定类型的实体集合

            /// </summary>

            /// <typeparam name="T">实体类型</typeparam>

            /// <param name="dateTimeToString">是否需要将日期转换为字符串,默认为转换,值为true</param>

            /// <returns></returns>

            public static List<T> ToList<T>(this DataTable dt, bool dateTimeToString) where T : class, new()

            {

                List<T> list = new List<T>();

                if (dt != null)

                {

                    List<PropertyInfo> infos = new List<PropertyInfo>();

                    Array.ForEach<PropertyInfo>(typeof(T).GetProperties(), p =>

                    {

                        if (dt.Columns.Contains(p.Name) == true)

                        {

                            infos.Add(p);

                        }

                    });

                    SetList<T>(list, infos, dt, dateTimeToString);

                }

                return list;

            }

            #region 私有方法

            private static void SetList<T>(List<T> list, List<PropertyInfo> infos, DataTable dt, bool dateTimeToString) where T : class, new()

            {

                foreach (DataRow dr in dt.Rows)

                {

                    T model = new T();

                    infos.ForEach(p =>

                    {

                        if (dr[p.Name] != DBNull.Value)

                        {

                            object tempValue = dr[p.Name];

                            if (dr[p.Name].GetType() == typeof(DateTime) && dateTimeToString == true)

                            {

                                tempValue = dr[p.Name].ToString();

                            }

                            try

                            {

                                p.SetValue(model, tempValue, null);

                            }

                            catch { }

                        }

                    });

                    list.Add(model);

                }

            }

            #endregion

    }

    /// <summary>

        /// 使用类的属性的Description属性对应DataTable中的字段名

        /// </summary>

        public static class TableToModel

        {

            /// <summary>

            /// 将DataRow类型转化为指定类型的实体

            /// </summary>

            /// <typeparam name="T">实体类型</typeparam>

            /// <returns></returns>

            public static T ToModel<T>(this DataRow dr) where T : class, new()

            {

                return ToModel<T>(dr, true);

            }

            /// <summary>

            /// 将DataRow类型转化为指定类型的实体

            /// </summary>

            /// <typeparam name="T">实体类型</typeparam>

            /// <param name="dateTimeToString">是否需要将日期转换为字符串,默认为转换,值为true</param>

            /// <returns></returns>

            /// <summary>

            public static T ToModel<T>(this DataRow dr, bool dateTimeToString) where T : class, new()

            {

                if (dr != null)

                    return ToList<T>(dr.Table, dateTimeToString).First();

                return null;

            }

            /// <summary>

            ///将DataTable类型转化为指定类型的实体集合

            /// </summary>

            /// <typeparam name="T">实体类型</typeparam>

            /// <returns></returns>

            public static List<T> ToList<T>(this DataTable dt) where T : class, new()

            {

                return ToList<T>(dt, true);

            }

            /// <summary>

            /// 将DataTable类型转化为指定类型的实体集合

            /// </summary>

            /// <typeparam name="T">实体类型</typeparam>

            /// <param name="dateTimeToString">是否需要将日期转换为字符串,默认为转换,值为true</param>

            /// <returns></returns>

            public static List<T> ToList<T>(this DataTable dt, bool dateTimeToString) where T : class, new()

            {

                List<T> list = new List<T>();

                if (dt != null)

                {

                    List<PropertyInfo> infos = new List<PropertyInfo>();

                    Array.ForEach<PropertyInfo>(typeof(T).GetProperties(), p =>

                    {

                        foreach (Attribute att in p.GetCustomAttributes(true))

                        {

                            System.ComponentModel.DescriptionAttribute dscript = att as System.ComponentModel.DescriptionAttribute;

                            if (dscript != null)

                            {

                                if (dt.Columns.Contains(dscript.Description) == true)

                                {

                                    infos.Add(p);

                                }

                            }

                        }

                    });

                    SetList<T>(list, infos, dt, dateTimeToString);

                }

                return list;

            }

            #region 私有方法

            private static void SetList<T>(List<T> list, List<PropertyInfo> infos, DataTable dt, bool dateTimeToString) where T : class, new()

            {

                foreach (DataRow dr in dt.Rows)

                {

                    T model = new T();

                    infos.ForEach(p =>

                    {

                        foreach (Attribute att in p.GetCustomAttributes(true))

                        {

                            System.ComponentModel.DescriptionAttribute dscript = att as System.ComponentModel.DescriptionAttribute;

                            if (dscript != null)

                            {

                                if (dr[dscript.Description] != DBNull.Value)

                                {

                                    object tempValue = dr[dscript.Description];

                                    if (dr[dscript.Description].GetType() == typeof(DateTime) && dateTimeToString == true)

                                    {

                                        tempValue = dr[p.Name].ToString();

                                    }

                                    try

                                    {

                                        p.SetValue(model, tempValue, null);

                                    }

                                    catch { }

                                }

                            }

                        }

                    });

                    list.Add(model);

                }

            }

            #endregion

        }

  • 相关阅读:
    SwiftyUserDefaults对NSUserDefaults的封装扩展
    干货-iOS、mac开源项目及库,以后我也会持续更新。
    swift
    swift 集成使用最新版百度地图_v2.10.2(一)
    Xcode git 忽略user interface state文件
    ios8 UITableView设置 setSeparatorInset:UIEdgeInsetsZero不起作用的解决办法(去掉15px空白间距)
    swift--字符串替换/过滤/切割
    干货分享--iOS及Mac开源项目和学习资料【超级全面】
    zookeeper和Eureka的区别
    Date工具遇到的一个坑
  • 原文地址:https://www.cnblogs.com/TNTZWC/p/1887299.html
Copyright © 2011-2022 走看看