private static void CompareEnumarableItemObject(IEnumerable sourceObject, IEnumerable targetObject, ObjectCompareResultCollection updated, ObjectCompareResultCollection addOrDeleted, Func <ObjectCompareInfo, object, object, ObjectCompareResult> createItemResult) { if (sourceObject != null) { foreach (object sourceValue in sourceObject) { if (sourceValue != null) { ObjectCompareInfo compareInfo = sourceValue.GetType().GetCompareInfo(); string[] compareKeyFields = compareInfo.GetCompareKeyFields(); object targetValue = targetObject.FirstOrDefault <object>(t => IsSameCompareKey(compareInfo.GetCompareKeyFields(), sourceValue, t)); ObjectCompareResult itemResult = createItemResult(compareInfo, sourceValue, targetValue); if (itemResult.AreDifferent) { ObjectCompareResultCollection resultCollection = GetCollectionByCompareValues(sourceValue, targetValue, updated, addOrDeleted); if (resultCollection != null) { resultCollection.Add(itemResult); } } } } } }
/// <summary> /// 根据比较前后的对象决定使用哪个集合 /// </summary> /// <param name="sourceValue"></param> /// <param name="targetValue"></param> /// <param name="updated"></param> /// <param name="addOrDeleted"></param> /// <returns></returns> private static ObjectCompareResultCollection GetCollectionByCompareValues(object sourceValue, object targetValue, ObjectCompareResultCollection updated, ObjectCompareResultCollection addOrDeleted) { ObjectCompareResultCollection result = updated; if (sourceValue == null && targetValue != null) //没有原对象,Add { result = addOrDeleted; } else if (targetValue == null && sourceValue != null) //没有新对象,Delete { result = addOrDeleted; } return(result); }