Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Case insensitive Contains with Dynamic Linq

When using Contains with Dynamic Linq on Linq-to-objects, the search is case sensitive. I would like to be able to search case insensitive (like Linq-to-sql, cause SQL server does this by default).

Something like:

this.someQuery = this.someQuery.Where(field + ".Contains(@0, true)", strValue);

where true means: caseinsensitive = true, like one of the extensions of System.String.Contains provides. Though i cannot use extensions to System.String with dynamic Linq by default.

like image 600
Roland Deschain Avatar asked Nov 22 '11 12:11

Roland Deschain


People also ask

How do you make a LINQ query case insensitive?

Use string. Equals(name, article.Name, StringComparison. OrdinalIgnoreCase) when you are sure that your database supports it. E.g. SQLite with a collate of NOCASE will ignore the option.

Is LINQ case sensitive?

LINQ StartsWith , EndsWith , and Contains are case sensitive and return false if two same string s are of different cases, e.g., " STRING " and " string ".

Can you use LINQ on dynamic?

It's possible to build up dynamic LINQ queries or queries with several conditional criteria. In fact there are several options for doing this, including the use of expression trees.

What is Dynamic LINQ?

The Dynamic LINQ library exposes a set of extension methods on IQueryable corresponding to the standard LINQ methods at Queryable, and which accept strings in a special syntax instead of expression trees.


1 Answers

Can you just .ToLower() both sides of the comparison? Something like this:

this.someQuery = this.someQuery.Where(field.ToLower().Contains(strValue.ToLower()));

Or did I misunderstand what you're looking for?

like image 161
JohnD Avatar answered Oct 26 '22 08:10

JohnD