using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
namespace Convert.Common
{
public static class DataTableConvert
{
/// <summary>
/// The DataTable type data into a List of <T> entities set t;将DataTable类型的数据转换成List<T>集合 T实体
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="dataTable"></param>
/// <returns></returns>
public static List<T> DataTableToList<T>(DataTable dataTable)
{
var list = new List<T>();
var targetType = typeof (T);
var allPropertyArray = targetType.GetProperties();
foreach (DataRow rowElement in dataTable.Rows)
{
var element = Activator.CreateInstance<T>();
foreach (DataColumn columnElement in dataTable.Columns)
{
foreach (var property in allPropertyArray)
{
if (property.Name.ToUpper().Equals(columnElement.ColumnName.ToUpper()))
{
if (rowElement[columnElement.ColumnName] == DBNull.Value ||
rowElement[columnElement.ColumnName] == "")
{
property.SetValue(element, null, null);
}
else
{
if (property.PropertyType == typeof (string))
{
property.SetValue(element, rowElement
[columnElement.ColumnName].ToString(), null);
}
else
{
//typeof(Nullable<decimal>)== typeof(decimal?)
if (property.PropertyType == typeof (decimal?) ||
property.PropertyType == typeof (decimal))
{
property.SetValue(element, decimal.Parse(rowElement
[columnElement.ColumnName].ToString()), null);
}
else if (property.PropertyType == typeof (short?) ||
property.PropertyType == typeof (short))
{
property.SetValue(element, short.Parse(rowElement
[columnElement.ColumnName].ToString()), null);
}
else if (property.PropertyType == typeof (int?) ||
property.PropertyType == typeof (int))
{
property.SetValue(element, int.Parse(rowElement
[columnElement.ColumnName].ToString()), null);
}
else if (property.PropertyType == typeof (DataAction?) ||
property.PropertyType == typeof (DataAction))
{
DataAction? da = null;
foreach (var name in Enum.GetNames(typeof (DataAction)))
{
if (
!string.IsNullOrEmpty(
rowElement[columnElement.ColumnName].ToString()))
{
if (name == rowElement[columnElement.ColumnName].ToString())
{
da =
(DataAction)
Enum.Parse(typeof (DataAction),
rowElement[columnElement.ColumnName]
.ToString(), true);
}
}
}
property.SetValue(element, da, null);
}
}
}
break;
}
}
}
list.Add(element);
}
return list;
}
public static DataSet ListToDataSet<T>(List<T> modelList)
{
if (modelList == null || modelList.Count == 0)
{
return null;
}
var ds = new DataSet();
ds.Tables.Add(ListToDataTable(modelList));
return ds;
}
public static DataTable ListToDataTable<T>(List<T> modelList)
{
if (modelList == null || modelList.Count == 0)
{
return null;
}
var dt = CreateData(modelList[0]);
foreach (var model in modelList)
{
var dataRow = dt.NewRow();
foreach (var propertyInfo in typeof (T).GetProperties())
{
dataRow[propertyInfo.Name] = propertyInfo.GetValue(model, null);
}
dt.Rows.Add(dataRow);
}
return dt;
}
/// <summary>
/// create DataTable by using model
/// </summary>
/// <param name="model">Class entity</param>
/// <returns></returns>
private static DataTable CreateData<T>(T model)
{
var dataTable = new DataTable(typeof (T).Name);
foreach (var propertyInfo in typeof (T).GetProperties())
{
try
{
dataTable.Columns.Add(new DataColumn(propertyInfo.Name.ToUpper(), propertyInfo.PropertyType));
}
catch (Exception)
{
dataTable.Columns.Add(new DataColumn(propertyInfo.Name.ToUpper(), typeof (string)));
}
}
return dataTable;
}
/// <summary>
/// change position of column in data table
/// </summary>
/// <param name="dt"></param>
/// <param name="columnsName"></param>
public static void ChangeDataTableColumnPosition(DataTable dt, string columnsName)
{
var columnNameArry =
columnsName.Trim(' ')
.Trim(' ')
.ToLower()
.Split(new[] {","}, StringSplitOptions.RemoveEmptyEntries)
.ToList();
var tbcol = new List<string>();
var delcol = new List<string>();
var addcol = new List<string>();
if (dt != null && dt.Rows.Count > 0 && columnNameArry.Count > 0)
{
foreach (DataColumn cl in dt.Columns)
{
if (!columnNameArry.Contains(cl.ColumnName.ToLower()))
{
delcol.Add(cl.ColumnName.ToLower());
}
else
{
tbcol.Add(cl.ColumnName.ToLower());
}
}
foreach (var clname in columnNameArry)
{
if (!tbcol.Contains(clname))
{
dt.Columns.Add(clname, typeof (string));
}
}
foreach (var dclname in delcol)
{
dt.Columns.Remove(dclname);
}
for (var i = 0; i < columnNameArry.Count; i++)
{
dt.Columns[columnNameArry[i]].SetOrdinal(i);
}
}
}
/// <summary>
/// change position of column in data table
/// </summary>
/// <param name="dt"></param>
/// <param name="columnsName"></param>
public static DataTable CreateDataTableColumnsName(string columnsName)
{
var dt = new DataTable();
var columnNameArry =
columnsName.Trim(' ')
.Trim(' ')
.ToLower()
.Split(new[] {","}, StringSplitOptions.RemoveEmptyEntries)
.ToList();
foreach (var clname in columnNameArry)
{
dt.Columns.Add(clname, typeof (string));
}
return dt;
}
}
}