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 > )
{
for (int n = ; 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 = ; 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( * * ))
}; 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);        }    }

datatable to List<T>带缓存的更多相关文章

  1. 带缓存的输入输出-bufferedinputstream类与bufferedoutputstream类

    package hengzhe.cn.o1; import java.io.*; /* * 带缓存的输入输出-bufferedinputstream类与bufferedoutputstream类 * ...

  2. 不带缓存的I/O和标准(带缓存的)I/O

    首先,先稍微了解系统调用的概念:       系统调用,英文名system call,每个操作系统都在内核里有一些内建的函数库,这些函数可以用来完成一些系统系统调用把应用程序的请求传给内核,调用相应的 ...

  3. 基于AFN封装的带缓存的网络请求

    给大家分享一个基于AFN封装的网络请求 git: https://github.com/zhouxihi/NVNetworking #带缓存机制的网络请求 各类请求有分带缓存 , 不带缓存, 可自定义 ...

  4. Delphi中带缓存的数据更新技术

    一. 概念 在网络环境下,数据库应用程序是c/s或者是多层结构的模式.在这种环境下,数据库应用程序的开发应当尽可能考虑减少网络数据传输量,并且尽量提高并发度.基于这个目的,带缓存的数据更新技术应运而生 ...

  5. 带标准IO带缓存区和非标准IO 遇到fork是的情况分析

    废话不多说 直接代码 #include<stdio.h> #include<sys/types.h> #include<unistd.h> #include< ...

  6. 不带缓存IO和标准(带缓存)IO

    linux对IO文件的操作分为: 不带缓存:open read.posix标准,在用户空间没有缓冲,在内核空间还是进行了缓存的.数据-----内核缓存区----磁盘 假设内核缓存区长度为100字节,你 ...

  7. 使用TP自带缓存时。出现第一次拿不到数据。

    使用TP自带缓存时.出现第一次拿不到数据. 仔细检查逻辑发现了问题所在. 逻辑:直接读缓存,如果没有从数据库查询,然后存入缓存. 问题出在以为$exchange = S($fileName,$exch ...

  8. 带缓存的基于DateTimeFormatter的日期格式化工具类

    JAVA中的SimpleDateFormat是非线程安全的,所有在1.8的JDK版本里提供了线程安全的DateTimeFormatter类,由于是线程安全的,故我们可以将此类缓存起来多次利用提高效率. ...

  9. Yii的自带缓存的使用

    Yii的自带缓存都继承CCache 类, 在使用上基本没有区别缓存基础类 CCache 提供了两个最常用的方法:set() 和 get().要在缓存中存储变量 $value,我们选择一个唯一 ID 并 ...

随机推荐

  1. DP专题:划分数问题

    一.这个专题有什么用 练练DP 练练组合数学 ...... 二.正题 此类问题有如下几种形态: 1. 将n划分成若干正整数之和的划分数.2. 将n划分成k个正整数之和的划分数.3. 将n划分成最大数不 ...

  2. Java 状态模式

    在阎宏博士的<JAVA与模式>一书中开头是这样描述状态(State)模式的:状态模式,又称状态对象模式(Pattern of Objects for States),状态模式是对象的行为模 ...

  3. db2 runstats和reorg

    当数据库里某张表上有大量插入操作时,需要在表上做 RUNSTATS 命令保证数据库掌握准确的统计信息. 当数据库里某张表中的记录变化很大时(大量插入.删除.更新操作),需要在表上做 REORG 和 R ...

  4. 使用html5 Canvas绘制线条(直线、折线等)

    使用html5 Canvas绘制直线所需的CanvasRenderingContext2D对象的主要属性和方法(有"()"者为方法)如下: 属性或方法 基本描述 strokeSty ...

  5. APP中的图片如何长按可以下载并保存图片到相册

    直接上图 方式一: 实现方式二: 方式三:

  6. TensorFlow实战Google深度学习框架8-9章学习笔记

    目录 第8章 循环神经网络 第9章 自然语言处理 第8章 循环神经网络 循环神经网络的主要用途是处理和预测序列数据.循环神经网络的来源就是为了刻画一个序列当前的输出与之前信息的关系.也就是说,循环神经 ...

  7. 关于Docker目录挂载的总结(转)

    关于Docker目录挂载的总结 Docker容器启动的时候,如果要挂载宿主机的一个目录,可以用-v参数指定. 譬如我要启动一个centos容器,宿主机的/test目录挂载到容器的/soft目录,可通过 ...

  8. 解决Protobuf生成的C#代码命名不规范问题

    起因 通常使用Protobuf的步骤为 定义 .proto 文件 使用 protoc 生成对应语言的代码 以生成C#代码为例,使用如下命令: protoc -I ../protos --csharp_ ...

  9. SQL Server 权限控制

    根据数据库Schema限制用户对数据库的操作行为 授予Shema dbo下对象的定义权限给某个用户(也就是说该用户可以修改架构dbo下所有表/视图/存储过程/函数的结构) use [Your DB N ...

  10. Source Insight小技巧:修改Symbol Window的默认宽度

    SI是个好东西,但是源代码窗口左边的符号窗口的默认宽度实在是太小,每次打开一个新的源码窗口都要重新拖放调整,很烦人.下面是一劳永逸调整Symbol Window宽度的方法. 打开一个源码窗口,将Sym ...