Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Like in Lambda Expression and LINQ

Tags:

c#

lambda

linq

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 I have something similar.

like image 835
Stacker Avatar asked Sep 01 '10 08:09

Stacker


People also ask

What is difference between LINQ and lambda expression?

Language Integrated Query (LINQ) is feature of Visual Studio that gives you the capabilities yo query on the language syntax of C#, so you will get SQL kind of queries. And Lambda expression is an anonymous function and is more of a like delegate type.

What is lambda expressions in LINQ?

Advertisements. The term 'Lambda expression' has derived its name from 'lambda' calculus which in turn is a mathematical notation applied for defining functions. Lambda expressions as a LINQ equation's executable part translate logic in a way at run time so it can pass on to the data source conveniently.

Can you use lambda expression instead of LINQ query?

So performance-wise, there's no difference whatsoever between the two. Which one you should use is mostly personal preference, many people prefer lambda expressions because they're shorter and more concise, but personally I prefer the query syntax having worked extensively with SQL.

Which is faster LINQ or lambda?

There is no performance difference between LINQ queries and Lambda expressions.


2 Answers

customers.Where(c => c.Name.Contains("john")); 
like image 131
Tom Vervoort Avatar answered Sep 21 '22 06:09

Tom Vervoort


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.

like image 41
Johannes Rudolph Avatar answered Sep 23 '22 06:09

Johannes Rudolph