views:

273

answers:

3

Hi,

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

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 ?

Thank you.

A: 

Are the delimiters or spaces around the data? You can use the .Like (InputKeyword) to refine it a bit more.

Jason N. Gaylord
I don't think that we have Like method.
Angkor Wat
You should have the Like method.
Jason N. Gaylord
+1  A: 

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 & " ")

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.

Read this: How to Combine LINQ with Regular Expressions

Jose Basilio
It'd be nice if you show us how to do this with Regex.
Angkor Wat
I'm not good at Regex, that's why, I provided the link for you. However, did you try using the where clause in my example?
Jose Basilio
Yes, I did and it worked. Thanks for your input. :D
Angkor Wat
I did mark it as the answer but I got the red notice saying "/content/img/vote-accepted.png from the site (click on this box to dismiss)". So, I clicked it several times.
Angkor Wat
A: 

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
eKek0
There's no delimiter. User can search anything like "2 cords", "cord of something".
Angkor Wat