LIKE with Linq to Entities

I know that the .Contains() method is similar to LIKE %therm% , the .StartsWith() method is similar to LIKE therm% and the .EndsWith() method is like LIKE %therm , but ...

Is there any way to do as shown below on **Linq to Entities** ?

 SELECT * FROM [dbo].[Users] WHERE Name LIKE 'rodrigo%otavio%diniz%waltenberg' 

PS: I USE LINQ TO COMPUTERS. NOT LINQ TO SQL

+6
c # sql linq linq-to-entities
source share
4 answers

Yes, you can do this using ESQL / Query Builder Syntax:

 var matching = Context.Users.Where("it.Name LIKE 'rodrigo%otavio%diniz%waltenberg'"); 
+2
source share

That 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:
Turns out this only works with LINQ2SQL, not LINQ2Entities. Linq SqlMethods.Like fail suggests you use Where directly in the table.

+2
source share

I posted my solution here , just in case it is here too:

Update 2019

For Entity Framework 6.2 you can use DBFunctions

For example:

 try { using (var db = new YOUREntities()) { var data = db.LenderProgram.Where(i => DbFunctions.Like(i.LenderProgramCode, "OTO%")) .ToList(); return data; } } catch (Exception e) { e.HandleException(); } 
0
source share

How to use regular expressions in a 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; } 
-one
source share

All Articles