检查List <string>是否包含另一个List <string>中的元素

时间:2017-03-17 07:42:19

标签: c# asp.net asp.net-mvc-4 c#-4.0

我有一个包含句子的列表。我有另一个具有特定单词的列表。如果句子中至少有一个单词,我想要列表中的句子。应该选择该句子并将其存储在变量中。

 List<string> features = new List<string>(new string[] { "battery", "screen", "audio" });

2 个答案:

答案 0 :(得分:2)

带有Any的Linq Contains应该是

List<string> features = new List<string>(){ "battery", "screen", "audio" };
List<string> sentences = new List<string>() { "this is a new screen", "i need a new battery", "there is no foo in my bar" };
List<string> result = sentences.Where(x => features.Any(y => x.Contains(y))).ToList();

答案 1 :(得分:-1)

你可以使用linq。

List<string> features = new List<string>(new string[] { "battery", "screen", "audio" });
// or simpler
//List<string> features = new List<string>{ "battery", "screen", "audio" };

List<string> listOfLines = GetAllLinesHereInList();
var filteredLines = listOfLines.Where(ln=>features.Any(f=>ln.IndexOf(f) > -1));
相关问题