1
\$\begingroup\$

Please have a look at the following 2 snippets:

var query = _ctx.Notes.Where(...);    
var searchLower = searchTerms.ToLower();
query = query.Where(a => a.Comment.ToLower().Contains(searchLower))

vs

var query = _ctx.Notes.Where(...);        
query = query.Where(a => a.Comment.ToLower().Contains(searchTerms.ToLower()))

Is there any peformance increase by converting the search terms before carrying out the LINQ query?

\$\endgroup\$
2

1 Answer 1

4
\$\begingroup\$

Instead of Contains(), use IndexOf() which takes a comparer:

var query = _ctx.Notes.Where(...);        
query = query.Where(a => a.Comment.IndexOf(searchTerms, StringComparison.OrdinalIgnoreCase) >= 0)
\$\endgroup\$
3
  • \$\begingroup\$ I think it should be >= 0 \$\endgroup\$ Dec 21, 2011 at 16:47
  • \$\begingroup\$ Is this actually quicker against an SQL database? \$\endgroup\$
    – jaffa
    Dec 21, 2011 at 17:07
  • \$\begingroup\$ @jaffa: your best bet for speed against the database is to have a case-insensitive column with Latin1_General_CS_AS (or appropriate) and run that contains in a stored proc. \$\endgroup\$ Dec 21, 2011 at 17:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.