I cache a specific list of EF entities in the application start routine of an asp.net mvc website.
protected void Application_Start()
{
CountriesDbContext db = new CountriesDbContext();
HttpRuntime.Cache["Countries"] = db.Countries.ToList();
}
When I request it like below I loose the accent insensitive behaviour
private List<Country> cache = (List<Country>)HttpRuntime.Cache["Countries"];
//Accent insensitive behaviour is lost !
results = cache.Where(c => c.FR.ToLower()
.Contains(search.ToLower())); //for example : e will not match é
If I request it “normally” using dbContext the accent insensitive behaviour is preserved.
private CountriesDbContext db = new CountriesDbContext();
//Accent insensitive behaviour search
results = db.Countries.Where(c => c.FR.ToLower()
.Contains(search.ToLower())); //for example : e will match é
Why is that ? and how can I do to preserve accent insensitive behaviour when caching entities ?
Collation is an SQL Server concept, which is why the query works on database data, but not in memory data, where .Net comparison applies.
So you need to implement an accent-insensitive comparer for your cache in .Net, as sketched in this answer.