LINQ查询来匹配多个单词 [英] LINQ query to match multiple words
问题描述
我一直在试图整天解决这个问题,并没有发现真正有效的解决方案。当我搜索了一些资料,我想根据多个单词筛选出来的数据。
I've been trying to solve this problem all day, and haven't found a solution that truly works. When I search for some data, I want to filter out the data based on multiple words.
我的输入值使用标准.Split功能分手了。
My input value is split up by using the standard .Split-function.
string[] searchstrings = MessageResult.Split(' ');
我做了一个查询(这显然不正常),它试图过滤掉所有在searchstrings每个字符串匹配的条目。
I've made a query (which obviously doesn't work properly) that tries to filter out all the entries that matches every string in searchstrings.
var suggestions = (from a in query
from w in searchstrings
where a.Message.ToLower().Contains(w.ToLower())
select a).Distinct();
查询是我的变量里面有所有的数据。我怎样才能让这个查询实际上只匹配出包括searchstrings每个字符串项?
query is my variable which has all the data. How can I make this query to actually only match out entries that includes every string in searchstrings?
推荐答案
我觉得下面的代码应该解决你的问题。它检查是否在搜索字符串的所有单词都在查询(一)。
I think code below should solve your problem. It checks if all words in searchstring are in a query (a).
var suggestions = (from a in query
where searchstrings.All(word => a.ToLower().Contains(word.ToLower()))
select a);
这篇关于LINQ查询来匹配多个单词的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!