日期:2014-05-17 浏览次数:20825 次
/// <summary>
/// 对比两个同类型的Collection<T>根据返回类型参数,返回差异Collection<T>集合
/// </summary>
/// <typeparam name="T">泛型类型</typeparam>
/// <param name="newCollection">修改后的新数据源</param>
/// <param name="oldCollection">原始数据源</param>
/// <param name="keyField">数据主键</param>
/// <param name="returnType">返回类型参数 0-新增加的数据 1-更新的数据 2-删除的数据 </param>
/// <returns></returns>
public Collection<T> GetDifferentCollections<T>(Collection<T> newCollection, Collection<T> oldCollection, string keyField, int returnType)
{
Collection<T> CollectionList = new Collection<T>();
//判断返回值类型
if (returnType == 0) //比较并获取新增的数据
{
foreach (T newCol in newCollection)
{
//取得新实体的数据主键值
object nob = newCol.GetType().GetProperty(keyField).GetValue(newCol, null);
T oldCol = oldCollection.Find((delegate(T old)
{
object ob = old.GetType().GetProperty(keyField).GetValue(old, null);
if (object.Equals(ob, nob))
return true;
&n