로직 개선
This commit is contained in:
@@ -155,26 +155,50 @@ namespace UVC.Data
|
||||
removedList.Clear();
|
||||
modifiedList.Clear();
|
||||
|
||||
// 현재 DataArray와 비교하여 변경된 항목을 추적
|
||||
for (int i = 0; i < Math.Max(this.Count, otherArray.Count); i++)
|
||||
// Id 기준으로 객체들을 비교하기 위한 사전 생성
|
||||
var thisDict = this.ToDictionary(item => item.Id, item => item);
|
||||
var otherDict = otherArray.ToDictionary(item => item.Id, item => item);
|
||||
|
||||
// 제거된 항목 확인 (현재 배열에는 있지만 other에는 없는 항목)
|
||||
foreach (var id in thisDict.Keys.Where(id => !otherDict.ContainsKey(id)))
|
||||
{
|
||||
if (i < this.Count && i < otherArray.Count)
|
||||
removedList.Add(thisDict[id]);
|
||||
}
|
||||
|
||||
// 추가된 항목 확인 (other에는 있지만 현재 배열에는 없는 항목)
|
||||
foreach (var id in otherDict.Keys.Where(id => !thisDict.ContainsKey(id)))
|
||||
{
|
||||
addedList.Add(otherDict[id]);
|
||||
}
|
||||
|
||||
// 수정된 항목 확인 (양쪽 모두에 있지만 내용이 다른 항목)
|
||||
foreach (var id in thisDict.Keys.Where(id => otherDict.ContainsKey(id)))
|
||||
{
|
||||
var thisItem = thisDict[id];
|
||||
var otherItem = otherDict[id];
|
||||
|
||||
if (!thisItem.ToString().Equals(otherItem.ToString()))
|
||||
{
|
||||
if (!this[i].ToString().Equals(otherArray[i].ToString()))
|
||||
{
|
||||
modifiedList.Add(this[i]);
|
||||
this[i].UpdateDifferent(otherArray[i]);
|
||||
}
|
||||
modifiedList.Add(thisItem);
|
||||
thisItem.UpdateDifferent(otherItem);
|
||||
}
|
||||
else if (i < this.Count)
|
||||
}
|
||||
|
||||
// 실제 컬렉션 업데이트
|
||||
// 현재 배열에서 제거된 항목들을 제거
|
||||
for (int i = this.Count - 1; i >= 0; i--)
|
||||
{
|
||||
if (removedList.Contains(this[i]))
|
||||
{
|
||||
removedList.Add(this[i]);
|
||||
}
|
||||
else if (i < otherArray.Count)
|
||||
{
|
||||
addedList.Add(otherArray[i]);
|
||||
this.RemoveAt(i);
|
||||
}
|
||||
}
|
||||
|
||||
// 추가된 항목들을 현재 배열에 추가
|
||||
foreach (var item in addedList)
|
||||
{
|
||||
this.Add(item);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user