Roland Deschain
Roland Deschain

Reputation: 709

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.

Upvotes: 14

Views: 8711

Answers (3)

xalien
xalien

Reputation: 173

If you want to use placeholders then your code should look like

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

Upvotes: 1

user3593847
user3593847

Reputation: 1

When using "toLower", make sure you include the () "ToLower()"

Upvotes: 0

JohnD
JohnD

Reputation: 14777

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?

Upvotes: 18

Related Questions