tags:

views:

87

answers:

5

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;
+3  A: 

Try using string.Contains () combined with EndsWith.

var results = from c in db.Customers
              where c.FullName.Contains (FirstName) && c.FullName.EndsWith (LastName)
              select c;
Kieron
A: 
 where c.FullName.Contains("string")
Praveen Prasad
+4  A: 

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.

madgnome
A: 

You can use contains:

string[] example = { "sample1", "sample2" };
var result = (from c in example where c.Contains("2") select c);
// returns only sample2
hwcverwe
+1  A: 

Try like this

var results = db.costumers.Where(X=>X.FullName.Contains(FirstName))
                          .Where(X=>X.FullName.EndsWith(LastName))
                          .Select(X=>X);
Pramodh