使用Linq to SQL搜索整个单词 [英] Search for whole word with Linq to SQL

查看:66
本文介绍了使用Linq to SQL搜索整个单词的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用VB.NET和Linq to SQL进行搜索查询.我正在使用Linq的Contains方法查找输入关键字.就像这样的note.note_content.Contains(InputKeyWord).

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).

问题在于,如果我搜索"cord",那么"according"也会出现.我希望结果仅与关键字匹配.

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.

如何使用Linq搜索整个关键字?

How do I do to search for the whole keyword with Linq ?

谢谢.

推荐答案

我假设inputKeyword可以出现在注释的开头,中间和结尾的任何位置,因此您需要有3个OR子句对于它可能出现的3个可能的地方.因此,您的LINQ where子句可能最终看起来像这样.

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.

note.note_content.StartsWith(InputKeyword & " ") OR _
note.note_content.EndsWith(" " & InputKeyword) OR _
note.note_content.Contains(" " & InputKeyword & " ")

这不是一个很好的解决方案,但它应该可以工作.如果您擅长使用正则表达式,那么LINQ也是一种选择.

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与正则表达式结合

这篇关于使用Linq to SQL搜索整个单词的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆