I think you should have a preperty called ID or Name , which you want to compare with searchPattern , and then use:
.Where(x => searchPattern == null || x.Name.Contains(searchPattern));
Because I'm trying to understand that x represents an entity, therefore, how do you want to compare the name entiy with the search pattern?
Edit:
After looking at the changes in the question, he still cannot use the ToString() method in this query because he cannot convert to an SQL expression.
You now have two options:
First: (I'm not sure if it depends on the expected size of the data), try loading all records from the database using the ToList() extension before calling the Where extension. This should work well, but it can cause a problem with huge tables.
Second: you need to create a stored procedure and move the logic to the database.
source share