Note that there are some explanatory texts on larger screens.

plurals
  1. POFull Search testing in LINQ (custom filter query)
    text
    copied!<p>I have a table that has a list of restaurant names and links to another table that holds the cuisine type.</p> <p>I want to provide a search box on my web page that when typing, each word is searched for in the database and results returned. I was looking for a solution that doesn't involve setting up sql full text search as I want to be able to test this using Linq to SQL.</p> <p>From my old code I have a function which creates a query filter given the input text and returns all the results based on that.</p> <pre><code>Private Function SetupQuery(ByVal searchText As String) As String Dim searchFields As New List(Of String) searchFields.Add("Name") searchFields.Add("Postcode") searchFields.Add("Cuisine") Dim firstCol As Boolean = True Dim a() As String Dim j As Integer a = searchText.Trim.Split(" ") Dim filter As String = "" Dim compareString As String For Each col As String In searchFields For j = 0 To a.GetUpperBound(0) compareString = a(j).ToUpper() compareString = compareString.Trim() If firstCol Then filter = filter &amp; col &amp; " LIKE '" &amp; compareString &amp; "%' " firstCol = False Else filter = filter &amp; " or " &amp; col &amp; " LIKE '" &amp; compareString &amp; "%' " End If Next Next Return filter End Function </code></pre> <p>This has the search fields hard coded and then loops through each one and each word in the search text to construct an OR LIKE filter.</p> <p>I'm sure I could then use this in my LINQ code but it doesn't seem like an elegant solution especially since the columns are hard coded and not used how LINQ uses them.</p> <p>Could anyone recomend a better way to do this or some tips in what direction to go with this? </p> <p>Thanks</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload