user476566
user476566

Reputation: 1349

how to use like operator in lambda expression

Hello question on like condition using lambda expression.

I hope you can reply to this post asap (Its little urgent)

My question is how to do like queries , i understand i can use startswith , contains etc or sqlmethods like . But how to use them in lambda expression.

For eg : Person entity contains name , in the below example, I search for person.name ='john'.

But the user may select any operator at run time for comparision (like , equals, starts with , ends with etc) Eg. it could be person.name ='john' or person.name like 'j%' or person.name like '%j' or person.name contains '%jo%'

how can I construct dyanmic query . can you please modify the code above and let me know. As I am quite new to linq , I dont understand lambda so well.

Your help is appreciated.

Source code -

  DataContext context = new DataContext // linq to sql data context.
  Query<Person> q = new Query<Person>(context);
  Expression<Func<Manufacturer, bool>> expn = DynamicExpression.ParseLambda<Manufacturer, bool>(condition, value); //condition is "name=@0",   value = string[] i.e - "john"
                q.Where = expn;
                return q.SingleOrDefault<Person>();

eg of expn is name =@0 , but i want to do name like '%@0%' how ?

Query class is as follows

public Expression<Func<T, bool>> Where
{
    set { m_where = value; }
}

Upvotes: 2

Views: 10053

Answers (2)

sundar
sundar

Reputation: 51

you may want to try this one!

list.exists(element => element.startswith("starting text to search in list"))

Upvotes: 5

leppie
leppie

Reputation: 117300

In Linq2SQL, you can use SqlMethods.Like.

Upvotes: -1

Related Questions