views:

85

answers:

3

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 SQL

+2  A: 

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. http://stackoverflow.com/questions/2584598/linq-sqlmethods-like-fails suggests that you can use Where directly on the table.

Albin Sunnanbo
Exception: LINQ to Entities does not recognize the method 'Boolean Like(System.String, System.String)' method, and this method cannot be translated into a store expression.
Rodrigo Waltenberg
@Rodrigo: Hmmm, l2e works to close to l2s to be confusing... I updated my answer with a linq to another question that might be helpful.
Albin Sunnanbo
+1  A: 

Yes, you can do this with ESQL/Query Builder syntax:

var matching = Context.Users.Where("it.Name LIKE 'rodrigo%otavio%diniz%waltenberg'");
Craig Stuntz
Worked! Thanks!
Rodrigo Waltenberg
A: 

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;
        }
Terry Camerlengo
Exception: LINQ to Entities does not recognize the method 'Boolean IsMatch(System.String)' method, and this method cannot be translated into a store expression.
Rodrigo Waltenberg