I want to search for this:
Post Cereal
and get this:
Post Honey Nut Cereal
where the wild cards would be the spaces.
I know I could do a SPLIT and a series of ANDs and Contains() and translation to a Linq Expression for each term as a specification object, but isn't there a way to honor wildcards in the term sent to SQL? I looked at SQL functions where it's in Linq to SQL, but I am not sure what it is in Linq to Entities.
I would like to do something like this:
term = '%' + term.Replace(' ', '%') + '%';
db.table.where( p => System.Data.Objects.SqlClient.SqlFunctions
.SqlMethods.Like(p.fieldname, term) );
Any suggestions?
I believe you could use SqlFunctions.PatIndex:
dt.Table.Where(p => SqlFunctions.PatIndex(term, p.fieldname) > 0);
SqlFunctions.PatIndex behaves the same as the SQL LIKE operator. It supports all standard wildcard characters including:
SqlFunctions.PatIndex is often available when the SqlMethods.Like is not available (including within MVC controllers)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With