【发布时间】:2021-04-16 07:53:33
【问题描述】:
假设我们有两个列表:
List<string> listA = new List<string>{"a", "c"};
List<string> listB = new List<string>{"a", "a", "b", "c", "d"};
我们想从 listB 中删除所有与 listA 重复的内容。
listA 应该保持不变
listB 应该留下元素 {"b", "d"}
一个明显的解决方案是使用循环进行迭代,但我想知道如何使用 System.Linq one-liner 来完成?
也许是……
listB.RemoveAll(x => x.Equals(??));
或者……
listA.ForEach(key => listB.RemoveAll(x => x.Equals(key))); // cannot convert string[] to void
【问题讨论】:
标签: c# linq collections