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); } }