排除列表

Bea*_*ear 0 c# linq

我正在寻找最优雅和有效的方法来从一个列表中排除元素,这些元素存在于另一个列表中.让我们说有:

List<A> As which contains more than 1 000 000 records
List<B> Bs which contains more than 100 000 records
Run Code Online (Sandbox Code Playgroud)

我需要从列表As中删除所有As.Id相等的元素Bs.Id.我只需要从包含相同Id的Bs中减少As的元素.操作我重复数百次.解决该问题的最有效(和优雅)方法是什么?我希望LINQNotIn.

foreach(a in As)
{
    if(Bs.Any(b => b.Id == a.Id)
        As.Remove(a);
}
Run Code Online (Sandbox Code Playgroud)

需要很长时间才能完成.

Dmi*_*nko 5

假设它A.IdB.Id类型一样int,你可以List<B>变成HashSet<int>(效率)而不是使用RemoveAll(它应该是优雅的吗?):

//TODO: I've assumed that Id is of type int, change HashSet<T> if required 
// Efficiency: HashSet.Contains has O(1) time complexity when List.Contains - O(n)
HashSet<int> IdsToExclude = new HashSet<int>(Bs
  .Select(b => b.Id)); // <- Linq (Select) is OK here...

// ... but not here (efficiency and if you don't mind, elegancy):
// We don't want Linq since Linq (Except) will create a new collection of about 1e6 items
// All we want is to modify the existing collection in place: RemoveAll
As.RemoveAll(a => IdsToExclude.Contains(a.Id));
Run Code Online (Sandbox Code Playgroud)