【发布时间】:2019-04-06 02:48:30
【问题描述】:
我有一个对象列表,其中包含一个名为“Color”的字符串属性。我需要使用空格分隔符将字符串拆分为一个列表,并将该列表与另一个列表进行比较,以查看是否有任何包含的字符串使用 Linq 匹配。
string searchString = "I like sand";
List<string> searches = searchString.Split(' ').ToList();
//Determine if matches exists anywhere between the 2 strings using linq
List<myObject> obj = myObjectList.Where(x=> searches.Any(a=>x.Color.Contains(a))).Any();
使用我当前的 Linq 查询,我只能找到完全匹配。因此,假设我的对象颜色属性等于“沙”,查询将返回匹配项,但如果我的颜色等于“沙丘”之类的两个单词名称,我的查询将不会返回匹配项。
这个例子应该有助于解释什么需要作为匹配返回。
//Two strings should return a match as the word sand is in both
"I like sand"
"sand dune"
//Two strings should NOT return a match as no common words exist
"I like sand"
"Ice cream"
感谢任何帮助。
【问题讨论】:
-
使用
Intersect()和Any()