【问题标题】:How do I use Linq to find the elements of a list that are not present in another list?如何使用 Linq 查找列表中不存在于另一个列表中的元素?
【发布时间】:2011-06-20 21:05:45
【问题描述】:

我有 2 个List String 变量:

List<string> stringList1
List<string> stringList2 

其中stringList2stringList1 的子集

现在我想要stringList1 上不在stringList2 中的所有元素

如何使用 Linq 实现这一目标?

【问题讨论】:

  • 如果您经常对许多项目执行此操作,请考虑使用集合 - 即时大哦改进。

标签: c# linq list ienumerable subset


【解决方案1】:

您想在IEnumerable&lt;T&gt; 上使用Except 扩展方法

var results = stringList1.Except(stringList2);

【讨论】:

    【解决方案2】:

    使用这个 LINQ 表达式:

    from string x in stringList1 where !stringList2.Contains(x) select x;
    

    我确定有一个内置方法,但那就是 LINQ。 (我现在没有 VC#...)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-07-07
      • 2022-01-06
      • 1970-01-01
      • 1970-01-01
      • 2011-08-17
      • 2019-03-25
      • 1970-01-01
      相关资源
      最近更新 更多