I need to implement search by term and store in IQueryable
but Entity Framework doesn't allow to use Contains
method in Linq-to-Entities, how can I replace it?
query = query.Where(e => e.Fullname.Contains(searchTerm, StringComparison.InvariantCultureIgnoreCase) ||
e.Email.Contains(searchTerm, StringComparison.InvariantCultureIgnoreCase) ||
e.Username.Contains(searchTerm, StringComparison.InvariantCultureIgnoreCase));
remove StringComparison.InvariantCultureIgnoreCase
query = query.Where(e => e.Fullname.Contains(searchTerm) ||
e.Email.Contains(searchTerm) ||
e.Username.Contains(searchTerm));
Linq to SQL translates Contains("searchterm")
into like '%searchterm%'
which is case insensitive
Entity Framework Core has a built in function EF.Functions
in the Microsoft.EntityFrameworkCore
namespace. You could use it as such
query = query.Where(e => EF.Functions.FreeText(e.Fullname, searchTerm) || EF.Functions.Contains(e.Email, searchTerm)); //etc
Note. To use this function FullTextSearch
index should be configured on the table columns. I personally use Postgres
and this is the documentation on how to configure FullTextSearch
index on an entity. Find out how to configure such indexes from you provider.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.