从列表中删除项目
本文关键字:删除项目 列表 | 更新日期: 2023-09-27 18:17:26
我有两个对象列表,我必须删除'原始'列表中删除的值在另一个列表;但是项目是由两个属性标识的。当对象仅由一个属性标识时,我能够这样做,但现在我需要检查两个属性
// library: object with deleted data
// library = new List<Widget>() { new Widget() { Id = "1", Nbr = 1 }, new Widget() { Id = "3", Nbr = 2 } };
var allData = GetData();
// allData = new List<Widget>() { new Widget() { Id = "1", Nbr = 1 }, new Widget() { Id = "2", Nbr = 1 }, new Widget() { Id = "3", Nbr = 2 } };
// var itemsToDelete = allData.Where(w => library.All(p => p.Id != w.Id)).ToList(); // I would do this, if the identifier would be only Id
var itemsToDelete = allData.Where(w => library.All(p => p.Id != w.Id && p.Nbr != w.Nbr)).ToList(); // I need to check for two properties and I'm getting zero coincidences
var itemsToDelete = allData.Where(w => !library.Any(p => p.Id == w.Id && p.Nbr == w.Nbr)).ToList();
应该是正确的,但不是最优的。(O ^ 2)