zoukankan      html  css  js  c++  java
  • datatable to List<T>带缓存

    public class DataHelper
        {
            //datarow 转换的类型缓存
            private static MemoryCache modelCash = MemoryCache.Default;
    
            /// <summary>
            /// 将DataTable集合转换为指定的对象集合
            /// </summary>
            /// <typeparam name="T">要转换成的对象类型</typeparam>
            /// <param name="dt">DataTable数据集合</param>
            /// <returns>指定的对象集合</returns>
            public static List<T> DataTableToList<T>(DataTable dt) 
                where T : new()
            {
                var modelList = new List<T>();
                int rowsCount = dt.Rows.Count;
                if (rowsCount > 0)
                {
                    for (int n = 0; n < rowsCount; n++)
                    {
                        var model = DataRowToModel<T>(dt.Rows[n]);
                        modelList.Add(model);
                    }
                }
    
                return modelList;
            }
    
            /// <summary>
            /// 将DataRow数据转换为指定的对象
            /// </summary>
            public static T DataRowToModel<T>(DataRow dr) 
                where T : new()
            {
                if (dr == null) return default(T);
    
                var isResetCash = false;
                var model = new T();
                var propertyHt = GetCashValue(model.GetType().Name) ?? new Hashtable();
    
                for (var i = 0; i < dr.Table.Columns.Count; i++)
                {
                    var fieldName = dr.Table.Columns[i].ColumnName;
                    var property = GetProperty(ref isResetCash, model, propertyHt, fieldName);
    
                    if (property != null)
                    {
                        var fullName = property.PropertyType.FullName;
                        if (fullName.Contains("Guid"))
                        {
                            if (!string.IsNullOrEmpty(dr[fieldName].ToString()))
                                property.SetValue(model, new Guid(dr[fieldName].ToString()), null);
                            else
                                property.SetValue(model, null, null);
                        }
                        else if (fullName.Contains("Double"))
                        {
                            if (!string.IsNullOrEmpty(dr[fieldName].ToString()))
                                property.SetValue(model, double.Parse(dr[fieldName].ToString()), null);
                            else
                                property.SetValue(model, null, null);
                        }
                        else if (fullName.Contains("Int32"))
                        {
                            if (!string.IsNullOrEmpty(dr[fieldName].ToString()))
                                property.SetValue(model, int.Parse(dr[fieldName].ToString()), null);
                            else
                                property.SetValue(model, null, null);
                        }
                        else if (fullName.Contains("System.DateTime"))
                        {
                            if (!string.IsNullOrEmpty(dr[fieldName].ToString()))
                                property.SetValue(model, Convert.ToDateTime(dr[fieldName]), null);
                            else
                                property.SetValue(model, null, null);
                        }
                        else if (fullName.Contains("System.Byte"))
                        {
                            if (!string.IsNullOrEmpty(dr[fieldName].ToString()))
                                property.SetValue(model, dr[fieldName], null);
                            else
                                property.SetValue(model, null, null);
                        }
                        else if (fullName.Contains("System.Boolean"))
                        {
                            if (!string.IsNullOrEmpty(dr[fieldName].ToString()))
                                property.SetValue(model, dr[fieldName], null);
                            else
                                property.SetValue(model, null, null);
                        }
                        else if (fullName.Contains("System.Single"))
                        {
                            if (!string.IsNullOrEmpty(dr[fieldName].ToString()))
                                property.SetValue(model, dr[fieldName], null);
                            else
                                property.SetValue(model, null, null);
                        }
                        else
                        {
                            property.SetValue(model, dr[fieldName].ToString(), null);
                        }
                    }
                }
    
                if (isResetCash) SetCashValue(model.GetType().Name, propertyHt);
                return model;
            }
    
            private static PropertyInfo GetProperty<T>(ref bool isResetCash, T model, Hashtable propertyHt, string fieldName)
            {
                PropertyInfo property = null;
                if (propertyHt != null)
                {
                    if (propertyHt.ContainsKey(fieldName))
                    {
                        property = propertyHt[fieldName] as PropertyInfo;
                    }
                    else
                    {
                        property = model.GetType().GetProperty(fieldName);
                        if (property != null)
                        {
                            propertyHt[fieldName] = property;
                            isResetCash = true;
                        }
                    }
                }
                else
                {
                    property = model.GetType().GetProperty(fieldName);
                    if (property != null)
                    {
                        propertyHt[fieldName] = property;
                        isResetCash = true;
                    }
                }
    
                return property;
            }
    
            private static Hashtable GetCashValue(string key)
            {
                if (modelCash.Contains(key))
                {
                    return modelCash[key] as Hashtable;
                }
                return null;
            }
    
            private static void SetCashValue(string key, Hashtable value)
            {
                CacheItemPolicy cip = new CacheItemPolicy()
                {
                    AbsoluteExpiration = new DateTimeOffset(DateTime.Now.AddMinutes(30 * 24 * 60))
                };
    
                modelCash.Set(key, value, cip);
            }
        }

    public class DataHelper    {        //datarow 转换的类型缓存        private static MemoryCache modelCash = MemoryCache.Default;
            /// <summary>        /// 将DataTable集合转换为指定的对象集合        /// </summary>        /// <typeparam name="T">要转换成的对象类型</typeparam>        /// <param name="dt">DataTable数据集合</param>        /// <returns>指定的对象集合</returns>        public static List<T> DataTableToList<T>(DataTable dt)             where T : new()        {            var modelList = new List<T>();            int rowsCount = dt.Rows.Count;            if (rowsCount > 0)            {                for (int n = 0; n < rowsCount; n++)                {                    var model = DataRowToModel<T>(dt.Rows[n]);                    modelList.Add(model);                }            }
                return modelList;        }
            /// <summary>        /// 将DataRow数据转换为指定的对象        /// </summary>        public static T DataRowToModel<T>(DataRow dr)             where T : new()        {            if (dr == null) return default(T);
                var isResetCash = false;            var model = new T();            var propertyHt = GetCashValue(model.GetType().Name) ?? new Hashtable();
                for (var i = 0; i < dr.Table.Columns.Count; i++)            {                var fieldName = dr.Table.Columns[i].ColumnName;                var property = GetProperty(ref isResetCash, model, propertyHt, fieldName);
                    if (property != null)                {                    var fullName = property.PropertyType.FullName;                    if (fullName.Contains("Guid"))                    {                        if (!string.IsNullOrEmpty(dr[fieldName].ToString()))                            property.SetValue(model, new Guid(dr[fieldName].ToString()), null);                        else                            property.SetValue(model, null, null);                    }                    else if (fullName.Contains("Double"))                    {                        if (!string.IsNullOrEmpty(dr[fieldName].ToString()))                            property.SetValue(model, double.Parse(dr[fieldName].ToString()), null);                        else                            property.SetValue(model, null, null);                    }                    else if (fullName.Contains("Int32"))                    {                        if (!string.IsNullOrEmpty(dr[fieldName].ToString()))                            property.SetValue(model, int.Parse(dr[fieldName].ToString()), null);                        else                            property.SetValue(model, null, null);                    }                    else if (fullName.Contains("System.DateTime"))                    {                        if (!string.IsNullOrEmpty(dr[fieldName].ToString()))                            property.SetValue(model, Convert.ToDateTime(dr[fieldName]), null);                        else                            property.SetValue(model, null, null);                    }                    else if (fullName.Contains("System.Byte"))                    {                        if (!string.IsNullOrEmpty(dr[fieldName].ToString()))                            property.SetValue(model, dr[fieldName], null);                        else                            property.SetValue(model, null, null);                    }                    else if (fullName.Contains("System.Boolean"))                    {                        if (!string.IsNullOrEmpty(dr[fieldName].ToString()))                            property.SetValue(model, dr[fieldName], null);                        else                            property.SetValue(model, null, null);                    }                    else if (fullName.Contains("System.Single"))                    {                        if (!string.IsNullOrEmpty(dr[fieldName].ToString()))                            property.SetValue(model, dr[fieldName], null);                        else                            property.SetValue(model, null, null);                    }                    else                    {                        property.SetValue(model, dr[fieldName].ToString(), null);                    }                }            }
                if (isResetCash) SetCashValue(model.GetType().Name, propertyHt);            return model;        }
            private static PropertyInfo GetProperty<T>(ref bool isResetCash, T model, Hashtable propertyHt, string fieldName)        {            PropertyInfo property = null;            if (propertyHt != null)            {                if (propertyHt.ContainsKey(fieldName))                {                    property = propertyHt[fieldName] as PropertyInfo;                }                else                {                    property = model.GetType().GetProperty(fieldName);                    if (property != null)                    {                        propertyHt[fieldName] = property;                        isResetCash = true;                    }                }            }            else            {                property = model.GetType().GetProperty(fieldName);                if (property != null)                {                    propertyHt[fieldName] = property;                    isResetCash = true;                }            }
                return property;        }
            private static Hashtable GetCashValue(string key)        {            if (modelCash.Contains(key))            {                return modelCash[key] as Hashtable;            }            return null;        }
            private static void SetCashValue(string key, Hashtable value)        {            CacheItemPolicy cip = new CacheItemPolicy()            {                AbsoluteExpiration = new DateTimeOffset(DateTime.Now.AddMinutes(30 * 24 * 60))            };
                modelCash.Set(key, value, cip);        }    }

  • 相关阅读:
    使用EFCore连接现有数据库
    C#面试题总结
    xamarin学习--发布apk安装包
    xamarin学习--导航参数注意事项
    centos8 安装 gitlab
    mvc添加全局过滤器
    Windows平台查看端口占用情况
    asp.net core cli---创建一个不启用https的项目
    asp.net core cli
    启动nuxt项目报错WARN node unsupported "node@v8.9.3" is incompatible with chalk@^4.1.0, expec...
  • 原文地址:https://www.cnblogs.com/liuqiyun/p/10736516.html
Copyright © 2011-2022 走看看