LINQ 中提供值集合并检查集合中是否有任何/所有这些值的方法是什么?
谢谢
最佳答案
您可以通过 .Intersect()
模拟它并检查交集是否包含所有必需的元素。我想这效率很低,但又快又脏。
List<T> list = ...
List<T> shouldBeContained = ...
bool containsAll = (list.Intersect(shouldBeContained).Count == shouldBeContained.Count)
或者您可以使用 .All() 来完成。我想这更高效、更清洁:
List<T> list = ...
List<T> shouldBeContained = ...
bool containsAll = (shouldBeContained.All(x=>list.Contains(x));
关于c# - 检查多个值(存储在专用集合中)是否在 LINQ 集合中,在查询中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2289599/