多選 找出 要新增 刪除 更新 項目
var originalList = this.HotfixRepo.Query(x => x.Id == id).ToList();
var nowList = hotfixTitles.Select(title => new Hotfix
{
Id = id,
Title = title,
Hotfix = hotfix
}).ToList();
var newList = nowList.Where(x =>
!originalList.Select(m => m.Hotfix).Contains(x.Hotfix)).ToList();
var deleteList = originalList.Where(x =>
!nowList.Select(c => c.Hotfix).Contains(x.Hotfix)).ToList();
var UpdateList = nowList.Where(x =>
originalList.Any(m => m.Hotfix == x.Hotfix && m.Title != x.Title)).ToList();
foreach (var newItem in newList)
{
this.HotfixRepo.Create(newItem);
}
foreach (var deleteItem in deleteList)
{
this.HotfixRepo.Remove(deleteItem);
}
foreach (var updateItem in UpdateList)
{
var t = originalList.Single(x => x.Id == updateItem.Id &&
x.Hotfix == updateItem.Hotfix);
t.FullTitle = updateItem.FullTitle;
}
public class HotfixInfo
{
public long Id { get; set; }
/// <summary>
/// KB123456
/// </summary>
public string Hotfix { get; set; }
/// <summary>
/// 201404 Windows安全性更新 (KB123456)
/// </summary>
public string FullTitle { get; set; }
}
public class CompareResult
{
public List<HotfixInfo> ToBeAdded { get; set; }
public List<HotfixInfo> ToBeUpdated { get; set; }
public List<HotfixInfo> ToBeDeleted { get; set; }
}
public static class CompareHelper
{
/// <summary>
/// 比對出 要新增 要修改 要刪除的資料
/// </summary>
/// <param name="nowList"></param>
/// <param name="originalList"></param>
/// <returns></returns>
public static CompareResult CompareList(List<HotfixInfo> nowList, List<HotfixInfo> originalList )
{
var result = new CompareResult();
result.ToBeAdded = nowList.Where(x => !originalList.Select(m => m.Hotfix).Contains(x.Hotfix)).ToList();
result.ToBeDeleted = originalList.Where(x => !nowList.Select(c => c.Hotfix).Contains(x.Hotfix)).ToList();
result.ToBeUpdated = nowList.Where(x => originalList.Any(m => m.Hotfix == x.Hotfix && m.FullTitle != x.FullTitle)).ToList();
return result;
}
}
如果內容有誤請多鞭策謝謝