Linq and lambda expression

What is the difference between LINQ and Lambda expressions? Are there any advantages to using lambda instead of linq queries?

+4
source share
3 answers

Linq is a language integrated query. When using linq, a small anonymous function is often used as a parameter. This little anonymous function is a lambda expression.

var q = someList.Where(a => a > 7); 

In the above query, a => a > 7 is a lambda expression. This is equivalent to writing a small utility method and passing this value to Where :

 bool smallMethod(int value) { return value > 7; } // Inside another function: var q = someList.Where(smallMethod); 

This means that your question cannot be answered. Linq and lambdas are not interchangeable, and lambdas is one of the technologies used to implement linq.

+14
source

LINQ is a language-integrated query where the lamda expression is similar to the Annonymous method for .Net 2.0.

You cannot compare them, maybe you are confused because LINQ is most often associated with the expression lamda.

You need to see this article: LINQ and Lamda Expression Basics

EDIT: (I'm not sure, but maybe you are looking for the difference between the query syntax and the Sytnax method )

 int[] numbers = { 5, 10, 8, 3, 6, 12}; //Query syntax: IEnumerable<int> numQuery1 = from num in numbers where num % 2 == 0 orderby num select num; //Method syntax: IEnumerable<int> numQuery2 = numbers.Where(num => num % 2 == 0).OrderBy(n => n); 

In the above example, taken from MSDN , the Sytnax method contains the expression lamda (num => num % 2 == 0) , which works as a method, takes a number as input, and returns true if they are even.

They are both similar, and according to Jon Skeet, they will both compile with similar code .

+2
source

In a nutshell:

LINQ is a query technology (Language Integrated Query). LINQ makes extensive use of lambda as arguments to standard query operator methods, such as the Where clause.

A lambda expression is an anonymous function containing expressions and statements. It is completely separate and distinct from LINQ.

0
source

All Articles