Personal computing discussed

Moderators: renee, SecretSquirrel, just brew it!

 
StefanVonS
Gerbil Elite
Topic Author
Posts: 553
Joined: Mon Aug 14, 2006 2:51 pm
Location: Strong Badia

Lightswitch - "Relaxed" Search Question

Thu Dec 11, 2014 9:00 pm

Hey guys! Building an app using Lightswitch to automate inventory and ordering. Running into a problem with searching for inventory by description. In SQL, I would concat LIKE statements using the OR operator, but in Lightswitch, I'm at a complete loss. My google-foo is quite good, but I'm coming up empty.
Example:
I have a table called "Items" The field I'd like to search is called "Description" My query has a parameter called "searchParam"
So for an example item, its description is "Jumbo Wax Ring with Horn".
If I search: "Wax Ring" or "Jumbo Wax", I'll get the intended item. However, if I were to search "Jumbo Ring" or "Wax Horn", I'll get no result. The search is very literal and I can't find an elegant way around this problem. Any suggestions would be greatly appreciated! FYI, Lightswitch uses LINQ instead of SQL.
Current environment: Visual Studio 2013, Lightswitch HTML Application Web (VB, LINQ, JS)
 
StefanVonS
Gerbil Elite
Topic Author
Posts: 553
Joined: Mon Aug 14, 2006 2:51 pm
Location: Strong Badia

Re: Lightswitch - "Relaxed" Search Question

Sun Dec 14, 2014 6:29 pm

if it helps clarify at all, this is how I was constructing this search when I could use SQL:

   
        searchWords = Split(txt_Search)
        If UBound(searchWords) = 0 Then
            strSQL = "SELECT * FROM Items WHERE Items.Description LIKE '*" & txt_Search & "*' OR Items.Barcode LIKE '*" & txt_Search & "*'"
        Else
            strSQL = "SELECT * FROM Items WHERE Items.Description LIKE '*" & txt_Search & "*' OR Items.Barcode LIKE '*" & searchWords(0) & "*'"
            Dim lCounter        As Long
            For lCounter = (LBound(searchWords) + 1) To UBound(searchWords)
                strSQL = strSQL & " " & searchType & " Items.Description LIKE '*" & searchWords(lCounter) & "*'"
            Next
        End If
 
meerkt
Gerbil Jedi
Posts: 1754
Joined: Sun Aug 25, 2013 2:55 am

Re: Lightswitch - "Relaxed" Search Question

Sun Dec 28, 2014 8:20 am

 
StefanVonS
Gerbil Elite
Topic Author
Posts: 553
Joined: Mon Aug 14, 2006 2:51 pm
Location: Strong Badia

Re: Lightswitch - "Relaxed" Search Question

Tue Jan 20, 2015 9:50 am

For anyone in the future encountering this, here was the solution:

C#:
char[] separators = { ',', ';', ' ' };

if (!String.IsNullOrWhiteSpace(searchWords)
{
       string[] words = searchWords.Split(separators, StringSplitOptions.RemoveEmptyEntries);
       query = query.Where(a => words.Any(b => a.Description.Contains(b)));
}


VB:
Dim separators As Char() = {",", ";", " "}
Dim words As String() = searchWords.Split(separators, StringSplitOptions.RemoveEmptyEntries)
query = query.Where(Function(a) (words.Any(Function(b) (a.Description.Contains(b)))))

Who is online

Users browsing this forum: No registered users and 1 guest
GZIP: On