Like in Lambda Expression and LINQ

后端 未结 6 1632
伪装坚强ぢ
伪装坚强ぢ 2020-12-08 09:45

How can I do something like this:

customers.where(c=>c.Name **like** \"john\");

I know this isn\'t possible but I was wondering how can

6条回答
  •  广开言路
    2020-12-08 10:36

    If you are targeting LINQ to SQL, use SqlMethods.Like:

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

    Explanation:

    The compiler will generate an expression tree from the statement above. Since LIKE is a SQL specific construct and not common to all LINQ Query providers, the SqlMethods class and its members are used as a "hint" for the expression compiler (compiles expression trees to SQL) to emit a LIKE statement.

提交回复
热议问题