Search code examples
c#linq-to-sql

How to do a LIKE query with linq?


How can i perform an LIKE query within Linq?

I have the following query i would like to execute.

var results = from c in db.costumers
              where c.FullName LIKE "%"+FirstName+"%,"+LastName
              select c;

Solution

  • You could use SqlMethods.Like(matchExpression,pattern)

    var results = from c in db.costumers
                  where SqlMethods.Like(c.FullName, "%"+FirstName+"%,"+LastName)
                  select c;
    

    The use of this method outside of LINQ to SQL will always throw a NotSupportedException exception.