public class ObjectConvertProvider
{
/// <summary>
/// Convert DataTable To List
/// </summary>
/// <typeparam name="T">Object Type</typeparam>
/// <param name="t">要转换成的object</param>
/// <param name="dataTable">要转换的表</param>
/// <returns>return List</returns>
public static List<T> ConvertToList<T>(T t, DataTable dataTable) where T : class
{
var properties = System.ComponentModel.TypeDescriptor.GetProperties(t);
var names = (from DataColumn dataColumn in dataTable.Columns select dataColumn.ColumnName).ToList();
var result = new List<T>();
foreach (DataRow row in dataTable.Rows)
{
var instance = Activator.CreateInstance(t.GetType()) as T;
foreach (System.ComponentModel.PropertyDescriptor property in properties)
{
if (!names.Contains(property.Name)) continue;
var dbValue = row[property.Name];
if (property.PropertyType.FullName.Equals("System.String"))
{
if (dbValue.GetType().ToString().Equals("System.DBNull"))
{
property.SetValue(instance, "");
continue;
}
if (dbValue.GetType().ToString().Equals("System.DateTime"))
{
property.SetValue(instance, Convert.ToDateTime(dbValue).ToString("yyyy-MM-dd"));
continue;
}
property.SetValue(instance, Convert.ToString(dbValue));
continue;
}
if (property.PropertyType.FullName.Equals("System.Double"))
{
dbValue = dbValue == DBNull.Value ? 0.00 : dbValue;
property.SetValue(instance, Convert.ToDouble(dbValue));
continue;
}
if (property.PropertyType.FullName.Equals("System.Int32"))
{
dbValue = dbValue == DBNull.Value ? 0 : dbValue;
property.SetValue(instance, Convert.ToInt32(dbValue));
continue;
}
if (property.PropertyType.FullName.Equals("System.Int64"))
{
dbValue = dbValue == DBNull.Value ? 0 : dbValue;
property.SetValue(instance, Convert.ToInt64(dbValue));
continue;
}
property.SetValue(instance, row[property.Name]);
}
result.Add(instance);
}
return result;
}
/// <summary>
/// Convert DataTable To ListNullable
/// </summary>
/// <typeparam name="T">Object Type</typeparam>
/// <param name="t">要转换成的object</param>
/// <param name="dataTable">要转换的表</param>
/// <returns>return List</returns>
public static List<T> ConvertToListNullable<T>(T t, DataTable dataTable) where T : class
{
var properties = System.ComponentModel.TypeDescriptor.GetProperties(t);
var names = (from DataColumn dataColumn in dataTable.Columns select dataColumn.ColumnName).ToList();
var result = new List<T>();
foreach (DataRow row in dataTable.Rows)
{
bool isadd = true;
var instance = Activator.CreateInstance(t.GetType()) as T;
foreach (System.ComponentModel.PropertyDescriptor property in properties)
{
if (!names.Contains(property.Name)) continue;
var dbValue = row[property.Name];
if (dbValue == DBNull.Value)
{
isadd = false;
break;
}
dbValue = dbValue == DBNull.Value ? null : dbValue;
property.SetValue(instance, dbValue);
}
if(isadd)
result.Add(instance);
}
return result;
}
/// <summary>
/// 将list中对象的日期类型转换为指定的格式
/// </summary>
/// <typeparam name="T">Object Type</typeparam>
/// <param name="list">要转换的List</param>
/// <param name="format">日期时间格式</param>
/// <param name="porpertyName">属性名</param>
/// <returns>return List</returns>
public static List<T> ConvertToDatetime<T>(List<T> list,string format,string porpertyName) where T : class
{
var result = new List<T>();
foreach (var t in list)
{
var properties = System.ComponentModel.TypeDescriptor.GetProperties(t);
foreach (System.ComponentModel.PropertyDescriptor property in properties)
{
if (property.Name.Equals(porpertyName))
{
property.SetValue(t, Convert.ToDateTime(property.GetValue(t)).ToString(format));
}
}
result.Add(t);
}
return result;
}
/// <summary>
/// Convert Enumerable to List
/// </summary>
/// <typeparam name="T">Object Type</typeparam>
/// <param name="t">要转换成的对象</param>
/// <param name="enumerable">要转换的集合</param>
/// <returns>return List</returns>
public static List<T> ConvertToList<T>(T t,IEnumerable enumerable) where T:class
{
var names = t.GetType().GetProperties().Select(property => property.Name).ToList();
var result = new List<T>();
foreach (var item in enumerable)
{
var instance = Activator.CreateInstance(t.GetType()) as T;
var properties = item.GetType().GetProperties();
foreach (var propertyInfo in properties)
{
if (!names.Contains(propertyInfo.Name)) continue;
var dbValue = propertyInfo.GetValue(item, null);
foreach (System.ComponentModel.PropertyDescriptor property in System.ComponentModel.TypeDescriptor.GetProperties(t))
{
if (property.Name.Contains(propertyInfo.Name))
{
property.SetValue(instance, dbValue);
break;
}
}
}
result.Add(instance);
}
return result;
}
}