0
uenlkr4e created
Hi, I have the following query:
var query = _recipientRepository.GetAll()
.WhereIf(!string.IsNullOrEmpty(input.Keyword), x => x.Name.Contains(input.Keyword) || x.LastName.Contains(input.Keyword)
|| x.Title.Contains(input.Keyword));
I thought .Contains was not case sensitive but it seems like it is. I am using postgresql so it maybe postgre related. Is there an easy way to turn ".contains" calls to case insensitive without going through each one and adding toLower()?
thank you
Markdown is supported
Copy & paste or drag & drop images (max 30 MB per image)
1 Answer(s)
-
0
Hi @uenlkr4e
- If you're using postgresql, you can replace Contains() with
EF.Functions.ILike(). This maps directly to the postgresqlILIKEoperator, which performs case insensitive string comparisons.
Example
var query = _recipientRepository.GetAll() .WhereIf(!string.IsNullOrEmpty(input.Keyword), x => EF.Functions.ILike(x.Name, $"%{input.Keyword}%") || EF.Functions.ILike(x.LastName, $"%{input.Keyword}%") || EF.Functions.ILike(x.Title, $"%{input.Keyword}%"));You can create a custom extension method to make it case insensitive. This way, you don’t need to modify your existing queries too much. You can call
ContainsIgnoreCaseinstead ofContains.Example
public static class QueryableExtensions { public static bool ContainsIgnoreCase(this string source, string toCheck) { return source != null && toCheck != null && source.IndexOf(toCheck, StringComparison.OrdinalIgnoreCase) >= 0; } }Markdown is supportedCopy & paste or drag & drop images (max 30 MB per image) - If you're using postgresql, you can replace Contains() with