I'm using VB.NET and Linq to SQL to do search query. I'm using the Contains method of Linq to look up the input keyword. It's like this note.note_content.Contains(InputKeyWord)
.
我正在使用VB.NET和Linq to SQL来进行搜索查询。我正在使用Linq的Contains方法来查找输入关键字。它就像这个note.note_content.Contains(InputKeyWord)。
The problem is that If I search for the "cord" then "according" will also come up. I want the result to be matched with keyword only.
问题是如果我搜索“绳子”,那么“相应”也会出现。我希望结果只与关键字匹配。
How do I do to search for the whole keyword with Linq ?
如何使用Linq搜索整个关键字?
Thank you.
3 个解决方案
#1
I'm assuming that the inputKeyword can appear at the beginning, any place in between and at the end of the note, therefore you need to have 3 OR clauses for the 3 possible places where it can appear. So, your LINQ where clause may end up looking like this.
我假设inputKeyword可以出现在开头,音符之间和音符末尾的任何位置,因此你需要在3个可能出现的地方有3个OR子句。所以,你的LINQ where子句可能最终看起来像这样。
note.note_content.StartsWith(InputKeyword & " ") OR _
note.note_content.EndsWith(" " & InputKeyword) OR _
note.note_content.Contains(" " & InputKeyword & " ")
This is not a pretty solution, but It should work. If you are adept with regular expressions, that's an option with LINQ as well.
这不是一个漂亮的解决方案,但它应该工作。如果您熟悉正则表达式,那么LINQ也是一个选项。
Read this: How to Combine LINQ with Regular Expressions
阅读:如何将LINQ与正则表达式结合使用
#2
Are the delimiters or spaces around the data? You can use the .Like (InputKeyword) to refine it a bit more.
数据周围是分隔符还是空格?您可以使用.Like(InputKeyword)来进一步细化它。
#3
I guess your keywords has delimiters on the beginning and on the end. If that's true, you could use
我猜你的关键字在开头和结尾都有分隔符。如果这是真的,你可以使用
from n in note
where n.note_content.Contains(beginDelimiter + InputKeyWord + endDelimiter) ||
n.note_content.StartsWith(InputKeyWord + endDelimiter) ||
n.note_content.EndsWith(beginDelimiter + InputKeyWord)
select n
#1
I'm assuming that the inputKeyword can appear at the beginning, any place in between and at the end of the note, therefore you need to have 3 OR clauses for the 3 possible places where it can appear. So, your LINQ where clause may end up looking like this.
我假设inputKeyword可以出现在开头,音符之间和音符末尾的任何位置,因此你需要在3个可能出现的地方有3个OR子句。所以,你的LINQ where子句可能最终看起来像这样。
note.note_content.StartsWith(InputKeyword & " ") OR _
note.note_content.EndsWith(" " & InputKeyword) OR _
note.note_content.Contains(" " & InputKeyword & " ")
This is not a pretty solution, but It should work. If you are adept with regular expressions, that's an option with LINQ as well.
这不是一个漂亮的解决方案,但它应该工作。如果您熟悉正则表达式,那么LINQ也是一个选项。
Read this: How to Combine LINQ with Regular Expressions
阅读:如何将LINQ与正则表达式结合使用
#2
Are the delimiters or spaces around the data? You can use the .Like (InputKeyword) to refine it a bit more.
数据周围是分隔符还是空格?您可以使用.Like(InputKeyword)来进一步细化它。
#3
I guess your keywords has delimiters on the beginning and on the end. If that's true, you could use
我猜你的关键字在开头和结尾都有分隔符。如果这是真的,你可以使用
from n in note
where n.note_content.Contains(beginDelimiter + InputKeyWord + endDelimiter) ||
n.note_content.StartsWith(InputKeyWord + endDelimiter) ||
n.note_content.EndsWith(beginDelimiter + InputKeyWord)
select n