Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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));
share|improve this question

3 Answers

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

u can try this it will work 100%

share|improve this answer
And how would it handle searchString1? Would it still do a contains or do I get an EndsWith() behaviour? – Johan 9 mins ago
ucan use ends with also like customers.where(a=>a.name.endswith("")) – navya 3 mins ago

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;
share
This isn't very dynamic... :/ – Johan 8 mins ago

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 should be string

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"));
share|improve this answer
And how would it handle searchString1? Would it still do a contains or do I get an EndsWith() behaviour? – Johan 8 mins ago

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.