To be clear, I am not referring to performing a .Cast<T>
on the results of an EF call.
I'm trying to implement a partial search on a number field.
When creating a SQL script, it is easy to convert a column to another data type:
SELECT Id
FROM Employees
WHERE CAST(Id AS VARCHAR) LIKE '%95%';
Is there any sort of equivalent in a LINQ query?
I can't modify the datatypes on the entity that I'm querying, and Id
was just for demonstration purposes.
I do not know why ToString doesn't work, but this is what works for me. Without needing SqlFunctions:
context.Employees.Where(emp => emp.Id.ToString().Contains("95")).ToList();
You can use SqlFunctions.StringConvert
:
var q = db.Employees
.Where(emp => SqlFunctions.StringConvert((decimal) emp.Id).Contains("95"));