0

I would like to know if something similar to this (linq to SQL)

customers.Where(c => SqlMethods.Like(c.Name, "%john%"));

is possible to do in Entity Framework. Preferably using lamba expressions.

My goal is to do something like this:

string searchString1 = "%foo";
string searchString2 = "%foo%";
string searchString3 = "foo";

customers.Where(c => SqlMethods.Like(c.Name, searchStringX));
Johan
  • 35,120
  • 54
  • 178
  • 293

4 Answers4

0

Custumers.where(c=>c.name.contains("foo"))

u can try this it will work 100%

navya
  • 144
  • 1
  • 1
  • 10
  • And how would it handle `searchString1`? Would it still do a contains or do I get an `EndsWith()` behaviour? – Johan Jun 14 '13 at 12:19
  • ucan use ends with also like customers.where(a=>a.name.endswith("")) – navya Jun 14 '13 at 12:26
0

Don't know anything new in lambda expressions but you could write a linq query like this:

var result = from i in customers 
         where i.name.Contains("yourString") 
         select i;

Also the datatype of the column should be varchar

Update: Just figured it out with lambda expressions

var result = customers.Where(c => c.name.StartsWith("yourString"));
var result = customers.Where(c => c.name.EndsWith("yourString"));
var result = customers.Where(c => c.name.Contains("yourString"));
konxie
  • 113
  • 2
  • 7
  • And how would it handle `searchString1`? Would it still do a contains or do I get an `EndsWith()` behaviour? – Johan Jun 14 '13 at 12:21
0

You could use the string contains, starts with and ends with methods to do the same thing:

string searchString1 = "foo"

var customerList = from x in customers where x.Name.Contains(searchString1) select x;

 var customerList = from x in customers where x.Name.StartsWith(searchString1) select x;

 var customerList = from x in customers where x.Name.EndsWith(searchString1) select x;
Ben Narube
  • 448
  • 3
  • 10
0

I ended up with the following solution:

.Where(c => SqlFunctions.PatIndex("%John", c.Name) > 0);
Johan
  • 35,120
  • 54
  • 178
  • 293