文斌的博客

学无止境
  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

DataTable To Entity

Posted on 2013-11-27 09:53  文斌1988  阅读(1836)  评论(1编辑  收藏  举报

using System;
using System.Collections.Generic;
using System.Data;
using System.Reflection;
using System.ComponentModel;


public static class DataTableUtility
{
/// <summary>
/// DataTable To IList<T>
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="dt"></param>
/// <returns></returns>
public static IList<T> ToList<T>(this DataTable dt)
{
if (dt == null || dt.Rows.Count == 0) return null;
IList<T> list = new List<T>();
foreach (DataRow row in dt.Rows)
{
T obj = row.ToEntity<T>();
list.Add(obj);
}
return list;
}

/// <summary>
/// DataRow To T
/// </summary>
public static T ToEntity<T>(this DataRow row)
{
Type objType = typeof(T);
T obj = Activator.CreateInstance<T>();

foreach (DataColumn column in row.Table.Columns)
{
PropertyInfo property =
objType.GetProperty(column.ColumnName,
BindingFlags.Public | BindingFlags.Instance | BindingFlags.IgnoreCase);
if (property == null || !property.CanWrite)
{
continue;
}
object value = row[column.ColumnName];
if (value == DBNull.Value) value = null;

property.SetValue(obj, value, null);

}
return obj;
}


/// <summary>
/// List To DataTable
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="list"></param>
/// <returns></returns>
public static DataTable ToDataTable<T>(this List<T> list)
{
try
{
Type objType = typeof(T);
DataTable dataTable = new DataTable(objType.Name);
if (list != null ? list.Count > 0 : false)
{
PropertyDescriptorCollection properties = TypeDescriptor.GetProperties(objType);
foreach (PropertyDescriptor property in properties)
{
Type propertyType = property.PropertyType;

//nullables must use underlying types
if (propertyType.IsGenericType && propertyType.GetGenericTypeDefinition() == typeof(Nullable<>))
propertyType = Nullable.GetUnderlyingType(propertyType);
//enums also need special treatment
if (propertyType.IsEnum)
propertyType = Enum.GetUnderlyingType(propertyType); //probably Int32

dataTable.Columns.Add(property.Name, propertyType);
}

foreach (T li in list)
{
DataRow row = dataTable.NewRow();
foreach (PropertyDescriptor property1 in properties)
{
row[property1.Name] = property1.GetValue(li) ?? DBNull.Value; //can't use null
}
dataTable.Rows.Add(row);

}
}
return dataTable;
}
catch
{
return null;
}
}
}

 -----------------------------------------------------------------两个实体比较,如果值为空则给其赋值

/// <summary>
/// 将新实体中的正则提取差异字段值付给原始数据
/// </summary>
/// <param name="newEntity"></param>
/// <returns></returns>
public RecordEntity CombindDiff(RecordEntity newEntity)
{
Type patternType = this.GetType();
Type newType = newEntity.GetType();
//获取这个实体的所有属性(字段,属性等 例如 Name,sex等实体中字段)
PropertyInfo[] patternPInfos = patternType.GetProperties();
foreach (PropertyInfo patternPInfo in patternPInfos)
{
//得到字段的属性列表
object[] customInfos = patternPInfo.GetCustomAttributes(typeof(PatternAttributes), true);
if (customInfos == null
|| customInfos.Length == 0)
continue;
//获取自定义属性头(实体)
PatternAttributes patternAtrributes = customInfos.GetValue(0) as PatternAttributes;
//容器和非正则提取字段都不进行比较
if (patternAtrributes.IsContainerPattern
|| !patternAtrributes.IsPattern)
{
continue;
}
//获取对比实体属性值
object newVal = null;
//RecordEntity newEntity
//Type newType = newEntity.GetType();
//通过属性名得到实体
PropertyInfo newPatternPInfo = newType.GetProperty(patternPInfo.Name);

if (newPatternPInfo != null)
newVal = newPatternPInfo.GetValue(newEntity, null);

if (patternAtrributes.IsInt32Pattern
|| patternAtrributes.IsDateTimePattern)
{
patternPInfo.SetValue(this, newVal, null);
}
else
{
//当前提取结果是空时
if (newVal == null
|| newVal.ToString() == "")
continue;

patternPInfo.SetValue(this, newVal, null);
}
}
return this;
}