I\'m trying to implement a search feature on a project using LINQ queries. Because the data sometimes contains characters with accents and other symbols, I created a method to r
When you use LINQ-to-Entities your LINQ query needs to be translated into a "server-side expression", which in English means something that the database can execute. The database doesn't know anything about your C# method called RemoveDiacritics
so you get an error at run-time.
You will need to execute the query first and then use LINQ-to-Objects to do the filtering. That can be done simply by adding a ToList()
to the query prior to filtering it. I'm a little more familiar with the fluent syntax, so I'd write it as:
q.ToList().Where(i =>
Util.StringUtil.RemoveDiacritics(i.entity.ToString().ToLowerInvariant()).Contains(search) ||
Util.StringUtil.RemoveDiacritics(i.name.ToString().ToLowerInvariant()).Contains(search));
You'll have to mess with it if you want the query syntax. It might be something like the following but I'm not 100% sure.
(from i in q.ToList()
where Util.StringUtil.RemoveDiacritics(i.entity.ToString().ToLowerInvariant()).Contains(search) ||
Util.StringUtil.RemoveDiacritics(i.name.ToString().ToLowerInvariant()).Contains(search));
Be aware, however, that this will bring back everything from the server and then perform the filtering client-side which may cause performance problems in your system depending on how much information is contained in your tables.