简体   繁体   English

从列表C#中删除项目

[英]Remove item from List C#

I have two list of Objects and I have to delete to the 'original' list the values deleted in the other list; 我有两个对象列表,我必须将在另一个列表中删除的值删除到“原始”列表中。 but the item is identified by two properties. 但是该商品由两个属性标识。 I was able to do so, when the object was only identified by one property, but now I need to check on two properties 当对象仅由一个属性标识时,我能够这样做,但是现在我需要检查两个属性

// 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(); 

Should be right, but not optimal. 应该是正确的,但不是最佳的。 (O^2) (O ^ 2)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM