Implement wildcard search

Viewed 45

Any thoughts how to add a wildcard search from the line similar to sql 'like%'. in my case to get the result, exact search gives the result. I need result's which match or like based on the searchkey

List = (from x in employee 
        where x.firstname.ToUpper().Contains(searchKey.ToUpper()) 
        || x.fullname.ToUpper().Contains(searchKey.ToUpper())  
        select x)
        .Take(5);
1 Answers

Unless you have changed the default collation (sorting/searching), it is very likely that SQL will do a case insensitive search by default. Using string.Contains will automatically be converted into LIKE %xxx%.

List = (from x in employee 
    where x.firstname.Contains(searchKey) 
    || x.fullname.Contains(searchKey)  
    select x)
    .Take(5);

string.StartsWith and string.EndsWith will also be converted to LIKE "%XXX" and LIKE "XXX%" respectively.

Related