Reputation: 12805
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.
Upvotes: 2
Views: 4867
Reputation:
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();
Upvotes: 4
Reputation: 4414
If you can't find a SqlFunctions
solution, there's always ExecuteQuery
var ids = db.ExecuteQuery<int>("SELECT Id FROM Employees WHERE CAST(Id AS VARCHAR) LIKE '%{0}%'", "95")
(This may not be exactly correct for the parameter.)
Upvotes: 2
Reputation: 460058
You can use SqlFunctions.StringConvert
:
var q = db.Employees
.Where(emp => SqlFunctions.StringConvert((decimal) emp.Id).Contains("95"));
Upvotes: 3