LIKE with Linq to Entities
I know the .Contains()
method does like LIKE %therm%
, the .StartsWith()
method does like LIKE therm%
and the .EndsWith()
method like LIKE %therm
but...
Is there a way to do like below on **Linq to Entities**
?
SELECT * FROM [dbo].[Users] WHERE Name LIKE 'rodrigo%otavio%diniz%waltenberg'
PS: I'M USING LINQ TO ENTITIES. NOT LINQ TO S开发者_如何学运维QL
Yes, you can do this with ESQL/Query Builder syntax:
var matching = Context.Users.Where("it.Name LIKE 'rodrigo%otavio%diniz%waltenberg'");
This should do the trick.
from u in context.users
where System.Data.Linq.SqlClient.SqlMethods.Like(
u.Name,
"rodrigo%otavio%diniz%waltenberg")
select u
Edit:
It turns out this only works with LINQ2SQL, not LINQ2Entities.
Linq SqlMethods.Like fails suggests that you can use Where
directly on the table.
How about using regular expressions with your LINQ statement? Something like the following:
RegularExpressions.Regex p
= new RegularExpressions.Regex("rodrigo%otavio%diniz%waltenberg");
using (DataContext.MyDataContext context = new MyDataContext())
{
var result = from u in context.users
where p.IsMatch(u.name)
select u;
}
精彩评论