C# List<object>.RemoveAll() - How to remove a subset of the list?

前端 未结 1 651
半阙折子戏
半阙折子戏 2020-12-10 15:54

I have a 2 classes feeds_Auto and Product with multiple matching properties. For this particular problem, the AutoID is the only field I n

1条回答
  •  一整个雨季
    2020-12-10 16:30

    You can use Any to simplify

    source.RemoveAll(a => dropSourceList.Any(b => a.AutoID == b.AutoID));
    

    You can reduce the looping by creating a HashSet of ID's first:

    var toRemove = new HashSet(dropSourceList.ConvertAll(a => a.AutoID));
    
    source.RemoveAll(a => toRemove.Contains(a.AutoID));
    

    0 讨论(0)
提交回复
热议问题