using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Reflection;
using System.Text; namespace TransformDTToModel
{
public class TransformUtil
{
/// <summary>
/// 将DB中改动的内容同步到泛型集合中
/// </summary>
/// <typeparam name="T">类型</typeparam>
/// <param name="source">dt源</param>
/// <param name="destinationArray">目标Model集合</param>
/// <returns></returns>
public static bool ConvertDataTableToModel<T>(DataTable source, List<T> destinationArray)
where T : class
{
if (source == null || destinationArray == null || source.PrimaryKey == null || source.PrimaryKey.Count() <= )
return false; DataTable dtChange = source.GetChanges();
if (dtChange == null)
return false; List<string> keys = new List<string>();
foreach (var item in source.PrimaryKey)
{
keys.Add(item.ColumnName);
} return ConvertDataTableToModel(source, destinationArray, keys);
} /// <summary>
/// 同步table里改动的数据到泛型集合里去(新增,修改,删除)
/// </summary>
/// <typeparam name="T">类型</typeparam>
/// <param name="source">dt源</param>
/// <param name="destinationArray">目标Model集合</param>
/// <param name="keyColumnArray">主键集合</param>
/// <returns></returns>
public static bool ConvertDataTableToModel<T>(DataTable source, List<T> destinationArray, List<string> keyColumnArray)
where T : class
{
if (source == null || destinationArray == null || source.Rows.Count == || keyColumnArray == null || keyColumnArray.Count == )
return false; Type modeType = destinationArray.GetType().GetGenericArguments()[];//模型类型
PropertyInfo[] ppInfoArray = modeType.GetProperties();//公共属性集合
List<PropertyInfo> listPPInfo = ppInfoArray.ToList();//方便查询
//关键列
List<PropertyInfo> keyPIArray = listPPInfo.FindAll(x => keyColumnArray.Contains(x.Name)); List<T> listToDelete = new List<T>();
//新增的数据
DataRow[] drAddArray = source.Select("", "", DataViewRowState.Added); object objItem = modeType.Assembly.CreateInstance(modeType.FullName);
foreach (DataRow dr in drAddArray)
{
destinationArray.Add((T)objItem);
foreach (System.Reflection.PropertyInfo pi in listPPInfo)
{
pi.SetValue(destinationArray[destinationArray.Count - ], dr[pi.Name], null);
}
}
//修改和删除的数据
DataView dvForOP = new DataView(source);
dvForOP.RowStateFilter = DataViewRowState.Deleted | DataViewRowState.ModifiedCurrent; foreach (DataRowView drv in dvForOP)
{
for (int i = ; i < destinationArray.Count; i++)
{
bool blIsTheRow = true;
//找出关键列对应的行
foreach (System.Reflection.PropertyInfo pInfo in keyPIArray)
{
object okey = pInfo.GetValue(destinationArray[i], null);
if (okey == null)
continue;
if (drv[pInfo.Name].ToString() != okey.ToString())
{
blIsTheRow = false;
break;
}
}
if (!blIsTheRow)//非本行
continue;
//根据行状态同步赋值
switch (drv.Row.RowState)
{
case DataRowState.Modified:
{
foreach (System.Reflection.PropertyInfo pi in listPPInfo)
{
if (keyPIArray.Contains(pi))//主键列不更新
continue;
pi.SetValue(destinationArray[i], drv[pi.Name], null);
}
} break;
case DataRowState.Deleted:
{
listToDelete.Add(destinationArray[i]);
} break;
}
}
} for (int i = ; i < listToDelete.Count; i++)
{
destinationArray.Remove(listToDelete[i]);
} return true;
} /// <summary>
/// 将视图转换成泛型集合
/// </summary>
/// <typeparam name="T">类型</typeparam>
/// <param name="dataView">视图</param>
/// <param name="model">泛型实例</param>
/// <returns></returns>
public static List<T> ConvertDataViewToModel<T>(DataView dataView, T model)
where T:class
{
List<T> listReturn = new List<T>();
Type modelType = model.GetType();
DataTable dt = dataView.Table;
//获取model所有类型
PropertyInfo[] modelProperties = modelType.GetProperties(); //遍历所有行,逐行添加对象
for (int i = ; i < dt.Rows.Count; i++)
{
object obj = modelType.Assembly.CreateInstance(modelType.FullName);
listReturn.Add((T)obj);
//遍历model所有属性
foreach (PropertyInfo pi in modelProperties)
{
//遍历所有列
foreach (DataColumn col in dt.Columns)
{
//如果列数据类型与model的数据类型相同、名称相同
if (col.DataType == pi.PropertyType
&& col.ColumnName == pi.Name)
{
pi.SetValue(obj, dt.Rows[i][col.ColumnName], null);
}
}
}
} return listReturn;
} /// <summary>
/// 将泛型集合类转换成DataTable
/// </summary>
/// <typeparam name="T">集合项类型</typeparam>
/// <param name="sourceArray">集合</param>
/// <param name="propertyNameArray">需要返回的列的列名,如需返回所有列,此参数传入null值</param>
/// <returns>数据集(表)</returns>
public static DataTable ConvertModelToDataTable<T>(IList<T> sourceArray, params string[] propertyNameArray)
where T:class
{
List<string> propertyNameList = new List<string>();
if (propertyNameArray != null)
propertyNameList.AddRange(propertyNameArray); DataTable result = new DataTable();
//获取结构
Type[] typeArr = sourceArray.GetType().GetGenericArguments();
if (typeArr.Length == )
return result; PropertyInfo[] propertys = typeArr[].GetProperties();
foreach (PropertyInfo pi in propertys)
{
if (propertyNameList.Count == )
{
result.Columns.Add(pi.Name, pi.PropertyType);
}
else
{
if (propertyNameList.Contains(pi.Name))
result.Columns.Add(pi.Name, pi.PropertyType);
}
}
for (int i = ; i < sourceArray.Count; i++)
{
ArrayList tempList = new ArrayList();
foreach (PropertyInfo pi in propertys)
{
if (propertyNameList.Count == )
{
object obj = pi.GetValue(sourceArray[i], null);
tempList.Add(obj);
}
else
{
if (propertyNameList.Contains(pi.Name))
{
object obj = pi.GetValue(sourceArray[i], null);
tempList.Add(obj);
}
}
}
object[] array = tempList.ToArray();
result.LoadDataRow(array, true);
} return result;
} }
}
TransformUtil
public class People
{
public string Id { get; set; } public string Name { get; set; } public string Address { get; set; }
}
单元测试
[TestClass]
public class UnitTestForTransformDTAndModel
{
[TestMethod]
public void TestConvertDataTableToModel()
{
DataTable dt = new DataTable();
dt.Columns.Add("Id", typeof(string));
dt.Columns.Add("Name", typeof(string));
dt.Columns.Add("Address", typeof(string));
dt.PrimaryKey = new DataColumn[] { dt.Columns[0] }; dt.Rows.Add("0001", "张三", "武汉市");
dt.Rows.Add("0002", "李四", "北京市");
dt.AcceptChanges();
dt.Rows.Add("0003", "王五", "深圳市"); List<People> allPeople = new List<People>(); TransformUtil.ConvertDataTableToModel<People>(dt, allPeople); //断言是不是只有一个数据,平且是只是修改状态的王五这个人
Assert.AreEqual(allPeople.Count, 1);
Assert.AreEqual(allPeople[0].Name, "王五");
} [TestMethod]
public void TestConvertModelToDataTable()
{
List<People> allPeople = new List<People>()
{
new People(){ Id="0001", Name="张三", Address ="武汉市"},
new People(){ Id="0002", Name="李四", Address ="北京市"},
new People(){ Id="0003", Name="王五", Address ="深圳市"}
}; DataTable dt = TransformUtil.ConvertModelToDataTable<People>(allPeople, null); //断言是不是有3行数据,数据的列有3列,第1列是不是Id,第一行第二列是不是张三
Assert.AreEqual(dt.Rows.Count, 3);
Assert.AreEqual(dt.Columns.Count, 3);
Assert.AreEqual(dt.Columns[0].ColumnName, "Id");
Assert.AreEqual(dt.Rows[0][1], "张三");
}
}