[英]How to check if list contains all string of another list
I have a object list. 我有一个对象列表。 That object contains a string List.
该对象包含一个字符串List。 Like that:
像那样:
public class ObjectA
{
...
IList<string> StringList;
}
And I have a list of words to search on StringList. 我有一个要在StringList上搜索的单词列表。 I need to search on a ObjectA list, and find all ObjectA that have all words (parts of all words).
我需要搜索一个ObjectA列表,并找到具有所有单词(所有单词的一部分)的所有ObjectA。
So I did that: 所以我做到了:
List<ObjectA> myObjectList;
List<string> wordsToFind;
var result = myObjectList.Where(objectA => wordsToFind.All(objectA.StringList.Contains));
The problem is my result is getting only whole words (equals). 问题是我的结果是只得到整个单词(等于)。 I would like to get results that contains parts of my wordsToFind.
我想得到包含我的wordsToFind部分内容的结果。
Example 例
wordsToFind = {"tes","don"};
StringListElement = {"test", "done"}
Should return on my select. 应该返回我的选择。
How can I do that? 我怎样才能做到这一点?
IndexOf
is probably where you want to be, or one of its overloads IndexOf
可能是您想要的地方,或其重载之一
Reports the zero-based index of the first occurrence of a specified Unicode character or string within this instance.
报告此实例中首次出现指定的Unicode字符或字符串的从零开始的索引。 The method returns -1 if the character or string is not found in this instance.
如果在此实例中找不到字符或字符串,则该方法返回-1。
List<ObjectA> myObjectList;
List<string> wordsToFind;
var result = myObjectList.Where(x => x.StringList.All(y => wordsToFind.Any(z => y.IndexOf(z) >= 0)));
Also note the time complexity of this is atrocious 另请注意,此操作的时间复杂度非常糟糕
Update 更新资料
Also note, if you want case insensitivity use 另请注意,如果要不区分大小写使用
y.IndexOf(z, StringComparison.InvariantCultureIgnoreCase)
or one of the following 或以下之一
CurrentCulture
Compare strings using culture-sensitive sort rules and the current culture. CurrentCulture
使用对区域性敏感的排序规则和当前区域性来比较字符串。
CurrentCultureIgnoreCase
Compare strings using culture-sensitive sort rules, the current culture, and ignoring the case of the strings being compared. CurrentCultureIgnoreCase
使用对区域性敏感的排序规则,当前区域性来比较字符串,而忽略要比较的字符串的大小写。
InvariantCulture
Compare strings using culture-sensitive sort rules and the invariant culture. InvariantCulture
使用区分文化的排序规则和不变区域性比较字符串。
InvariantCultureIgnoreCase
Compare strings using culture-sensitive sort rules, the invariant culture, and ignoring the case of the strings being compared. InvariantCultureIgnoreCase
使用对区域性敏感的排序规则,不变区域性来比较字符串,并忽略要比较的字符串的大小写。
Ordinal
Compare strings using ordinal (binary) sort rules. Ordinal
使用序数(二进制)排序规则比较字符串。
OrdinalIgnoreCase
Compare strings using ordinal (binary) sort rules and ignoring the case of the strings being compared. OrdinalIgnoreCase
使用序数(二进制)排序规则比较字符串,而忽略要比较的字符串的大小写。
I think this should work for you 我认为这应该为您工作
var wordsToFind = new List<string>{ "tes","don"};
var data= new List<ObjectA>()
{
new ObjectA()
{
StringList = new List<string>{"test", "done", "blah"}
},
new ObjectA()
{
StringList = new List<string>{"test2", "done2", "blah2"}
}
};
var result = (from item in data.Select(x => x.StringList)
from bar in item
from word in wordsToFind
where bar.Contains(word)
select bar)
.ToList();
The result should give you 结果应该给你
"test", "done","test2", "done2"
The ways to do this is by using a combination of Any
and All
. 做到这一点的方法是结合使用
Any
和All
。
You need to check if all the elements of wordsToFind
are substring of any elements in StringList
您需要检查的所有元素
wordsToFind
被串在任何元素StringList
bool result = wordsToFind.All(word => currentObject.StringList.Any(str => str.Contains(word));
This is for one object out of the list of Objects. 这是对象列表中的一个对象。 You can again apply
All
to that list. 您可以再次将
All
应用于该列表。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.