Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a way to cast in the `Where` part of an Entity Framework query?

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.

like image 590
krillgar Avatar asked Jan 16 '26 22:01

krillgar


1 Answers

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();

Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!