What is the easiest way to filter elements with LINQ through the Where method ignoring accentuation and case?
So far, I’ve been able to ignore Casing by calling methods on the properties, which I dont think is a good idea because it calls the same method for every element (right?).
So here’s what I got so far:
var result = from p in People
where p.Name.ToUpper().Contains(filter.ToUpper())
select p;
Please tell me if this is a good practice, and the easiest way to ignore accentuation.
To ignore case and accents (diacritics) you can first define an extension method like this:
(Modified from Ignoring accented letters in string comparison)
Now you can run your query:
This is fine if you are just iterating over a collection in C#, but if you are using LINQ to SQL it is preferable to avoid non-standard methods (including extension methods) in your LINQ query. This is because your code cannot be converted into valid SQL and hence run on SQL Server with all its lovely performance optimization.
Since there doesn’t seem to be a standard way of ignoring accents within LINQ to SQL, in this case I would suggest changing the field type that you want to search to be case- and accent-insensitive (CI_AI).
With your example:
Your query should now ignore accentuation and case.
Note that you will need to temporarily remove any unique constraints on the field before running the above query, e.g.
Now your LINQ query would simply be:
See related question here.