提问者:小点点

如何检查列表<string>是否存在其他列表<string>中的所有值


我正在试图弄清楚如何检查所有值是否与其他列表值匹配。

示例:

List<string> foo = new List<string>() {"banana","apple","coconut"}
List<string> bar = new List<string>() {"banana","apple","coconut","mango", "lemon"}

如果每个foo列表项都存在于bar中,则返回true,如果至少有一个不在foo列表中,则返回false。和if示例:

List<string> foo = new List<string>() {"banana",}
List<string> bar = new List<string>() {"banana","apple","coconut","mango", "lemon"}

应返回true

和if示例:

List<string> foo = new List<string>() {"banana","melon"}
List<string> bar = new List<string>() {"banana","apple","coconut","mango", "lemon"}

应返回false

和if示例:

List<string> foo = new List<string>() {"banana","apple","coconut","mango", "lemon"}
List<string> bar = new List<string>() {"banana"}

应返回false

对所有的变化感到抱歉。这件事得快点:D


共2个答案

匿名用户

使用intersect(),这实际上是非常直接的

foo.Intersect(bar).Count() == foo.Count()

匿名用户

尝试使用

var matching = !foo.Except(bar).ToList().Any();