繁体   English   中英

如何测试列表 a 的所有项目都在列表 b 中

[英]How to test that all items of list a are in list b

我有一组搜索词和一个 setence。 我需要测试该句子是否包含所有搜索词:

var searchTerms = "fox jumped".Split(' ');
var sentence = "the quick brown fox jumped over the lazy dog".Split(' ');
var test = sentence.Contains(searchTerms);

我期望测试为真——但是我得到一个编译错误:'string[]'不包含'Contains'的定义和最好的扩展方法重载'System.Linq.Queryable.Contains(System.Linq.IQueryable, TSource)' 有一些无效的参数

我应该如何测试该句子包含所有搜索词?

您正在尝试检查是否有任何未出现在句子中的搜索词:

if (!searchTerms.Except(sentence, StringComparer.CurrentCultureIgnoreCase).Any())

你可以这样做:

//test if all of the terms are in the sentence
searchTerms.All(term => sentence.Contains(term));

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM