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 > )
{
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>带缓存的更多相关文章
- 带缓存的输入输出-bufferedinputstream类与bufferedoutputstream类
package hengzhe.cn.o1; import java.io.*; /* * 带缓存的输入输出-bufferedinputstream类与bufferedoutputstream类 * ...
- 不带缓存的I/O和标准(带缓存的)I/O
首先,先稍微了解系统调用的概念: 系统调用,英文名system call,每个操作系统都在内核里有一些内建的函数库,这些函数可以用来完成一些系统系统调用把应用程序的请求传给内核,调用相应的 ...
- 基于AFN封装的带缓存的网络请求
给大家分享一个基于AFN封装的网络请求 git: https://github.com/zhouxihi/NVNetworking #带缓存机制的网络请求 各类请求有分带缓存 , 不带缓存, 可自定义 ...
- Delphi中带缓存的数据更新技术
一. 概念 在网络环境下,数据库应用程序是c/s或者是多层结构的模式.在这种环境下,数据库应用程序的开发应当尽可能考虑减少网络数据传输量,并且尽量提高并发度.基于这个目的,带缓存的数据更新技术应运而生 ...
- 带标准IO带缓存区和非标准IO 遇到fork是的情况分析
废话不多说 直接代码 #include<stdio.h> #include<sys/types.h> #include<unistd.h> #include< ...
- 不带缓存IO和标准(带缓存)IO
linux对IO文件的操作分为: 不带缓存:open read.posix标准,在用户空间没有缓冲,在内核空间还是进行了缓存的.数据-----内核缓存区----磁盘 假设内核缓存区长度为100字节,你 ...
- 使用TP自带缓存时。出现第一次拿不到数据。
使用TP自带缓存时.出现第一次拿不到数据. 仔细检查逻辑发现了问题所在. 逻辑:直接读缓存,如果没有从数据库查询,然后存入缓存. 问题出在以为$exchange = S($fileName,$exch ...
- 带缓存的基于DateTimeFormatter的日期格式化工具类
JAVA中的SimpleDateFormat是非线程安全的,所有在1.8的JDK版本里提供了线程安全的DateTimeFormatter类,由于是线程安全的,故我们可以将此类缓存起来多次利用提高效率. ...
- Yii的自带缓存的使用
Yii的自带缓存都继承CCache 类, 在使用上基本没有区别缓存基础类 CCache 提供了两个最常用的方法:set() 和 get().要在缓存中存储变量 $value,我们选择一个唯一 ID 并 ...
随机推荐
- java第十三周测试记录
今天课上遇到了问题,在我的上一篇随笔,这个阻碍了我很长时间,而且上一次也是这个问题,真的吃一堑不长一智,这次我应该就记住了,嗯. 设计思路: 俩个库: 1.一个库存商品,商品的基本属性和商品的数量(数 ...
- Spring MVC4 + Spring Security4 + Hibernate实例
http://www.yiibai.com/spring-security/spring-mvc-4-and-spring-security-4-integration-example.html 在这 ...
- boost::lockfree::queue多线程读写实例
最近的任务是写一个多线程的东西,就得接触多线程队列了,我反正是没学过分布式的,代码全凭感觉写出来的,不过运气好,代码能够work= = 话不多说,直接给代码吧,一个多消费者,多生产者的模式.假设我的任 ...
- jmeter接口测试实例6-注册(参数化)
Jmeter实例6:注册(参数化) 选中http协议,添加CSV Data set Config 准备参数中要使用到的值,存放到txt中,如果一个里面有多个参数,中间用,号分隔: 选中CSV元件,fi ...
- hiredis 使用 linux c++
1.linux下如何安装hiredis 1)下载地址 https://github.com/redis/hiredis 2)编译和安装 解压后的文件夹执行 make;make install; 3) ...
- Tornado-cookie
cookie 服务端在客户端的中写一个字符串,下一次客户端再访问时只要携带该字符串,就认为其是合法用户. tornado中的cookie有两种,一种是未加密的,一种是加密的,并且可以配置生效域名.路径 ...
- pygame-KidsCanCode系列jumpy-part6-主角挂掉重新开始
游戏的虚拟世界中,最让人happy的一个因素就是主角挂了,而且重来,只要restart就行了,不象现实中人的生命只有1次.回顾上节的效果,如果方块向下落时,挡板没接住,整个游戏就跪了: 如果我们希望方 ...
- html的文字样式、下行线、删除线、上标、下标等实现方式
先看效果如下: 代码如下: <del>del标签删除线</del><br/> <strike>strike标签删除线</strike>< ...
- Ubuntu 18.04 安装Virtual Box or VMWare workstation Pro 14
Linux相关的知识:https://www.cnblogs.com/dunitian/p/4822808.html#linux Virtual Box:sudo apt-get install vi ...
- MOS管当开关控制时,为什么一般用PMOS做上管NMOS做下管?
了解MOS管的开通/关断原理你就会发现,使用PMOS做上管.NMOS做下管比较方便.使用PMOS做下管.NMOS做上管的电路设计复杂,一般情况下意义不大,所以很少采用. 下面先了解MOS管的开通/关断 ...