开发者

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&开发者_Python百科lt;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; }
}


you may want to try this one!

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


In Linq2SQL, you can use SqlMethods.Like.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜