What is the difference between "lambda" and LINQ? How to differentiate them in a sentence?

Asked

Viewed 4,960 times

24

I often see terms like LINQ query and expressions lambda.

Then the question arose, What I’m doing is a LINQ query, an expression lambda or both?

Ex1:

var query = Produtos.Where(p => p.Descr.StartsWith("A")).Take(10);

Ex2:

var query = from produto in Produtos.Take(10)
where produto.Descr.StartsWith("A")
select new Produto 
{ Id = produto.Id, Descr = produto.Descr};
  • Only a curiosity a Linq expression using query syntax is converted by the compiler to a Method syntax, so a Query Syntax always has an equivalent form in Method Syntax, but the inverse does not occur, there are also differences between Query Syntax of C# and VB.NET, this second has a wider deployment, such as allowing Skip, take, while, etc... While in C# it is possible only with Method Syntax.

2 answers

21


LINQ is one thing and has two different syntaxes:

  • a is the query syntax or form declarative and that many people think that just this is LINQ (their second example)
  • another is the method or form syntax imperative that many people think is a lambda (your first example)

It is already clear that the two forms are LINQ, one in more natural language and the other more similar to what we normally program. The second way usually uses Amble, to represent the codes needed to execute the expression. Although you can understand, we cannot simply call this lambda since this is only a mechanism used to form the entire LINQ expression. It is worth remembering that the most declarative form also uses lambda, but in a more disguised way being the code passed by clause rather than method argument as in imperative form.

Differentiation occurs by the way it is written. The declarative form looks like the programming language, it is a more natural reading. In imperative form are used the full-dot, parentheses, and input methods of receiving parameters from lambda.

To learn more, understand the advantages of each way, I’ve already answered this question.

7

LINQ uses Amble, but Amble can be used without LINQ as well. Example:

//declara uma função que retorna um bool, para ver se um int tem todos os mesmos números
public static bool TodosIguais( this int num, Func<T,bool> igual ) {
  return igual(num);
}

//usa essa função, mas posso mandar qualquer função que retorna um bool
int numero = 55;

//Usando um lambda mais complexo:
bool todosIguais = numero.TodosIguais( i => {
    char comparar = num.ToString()[0];
    foreach( var n in num.ToString() ) {
      if ( comparar != n ) { return false; }
    }
    return true;
  } );

//Usando um lambda um pouco mais simples
todosIguais = numero.TodosIguais( 
  i => i.ToString().All(c=>c.Equals(i.ToString().First())) );

//Usando um lambda super simples, mas que possa retornar algo errado
todosIguais = numero.TodosIguais( i => i == i );

In other words, the lambda is a succinct command to declare a function, being the function parameters before the =>, and the content of the function after.

  • If you could set an example with practical use it would be better.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.