This question already has an answer here:
-
Is it Linq or Lambda?
2 answers
Can someone explain me the difference between lambda and linq?
Please don't point me out to other stackexchange answers or trivial explanations, I've checked most of them and they're orribly confusing.
I've used a bit of LINQ (I believe?) in these days with expressions like (merely an invented example)
var result = object.Where(e => e.objectParameter > 5).Any()
Which, should return in result a boolean which says if there is any element >5.
Ok, then, what are LINQ and lambda?
Is LINQ just a library, a set of functions, developed by C# team to include with
using System.Linq;
which gives you a powered "for loop" with many methods to avoid you getting your hands "dirty"? (First, FirstOrDefault, Any.... etc)
And what is Lambda? Is the same as above? It's a language on it's own? What is it and how it differs from LINQ? How do I recognize one or another?
Thanks
Language-Integrated Query (LINQ) is a set of features introduced in
Visual Studio 2008 that extends powerful query capabilities to the
language syntax of C# and Visual Basic
A lambda expression is an anonymous function that you can use to
create delegates or expression tree types. By using lambda
expressions, you can write local functions that can be passed as
arguments or returned as the value of function calls.
Linq uses Lambda expression in order to execute some of its functionalities.
Example:
new [] { "Dan", "Yossi", "Ben" }.Where(item => item.Length == 3);
Lambda expression: item => item.Length == 3
Linq: (from item in (new [] { "Dan", "Yossi", "Ben" }) where item.Length == 3)
Linq (Language Integrated Query) can use Lambdas (Lambda Expressions) but doesn't have to.
This is Linq:
var a = from b in someList
where b.Value == something
select b;
But can be written with a Lambda:
var a = someList.Where(b => b.Value == something);
The Lambda is b => b.Value == something
.
Where as mock.Setup(m => m.SomeOp()).Returns(new Thing());
uses a Lambda (the m => m.SomeOp()
), but has nothing to do with Linq.
LINQ is a library that contains a set of extension methods (mostly to IEnumerable) that makes heavy uses of lambdas. The lambda is itself an anonymous delegate and is just simple syntax sugar. It gives you an easy way to define a function "in line" and pass that function to some method.
for example you can write the above code like this:
var result = object.Where(delegate(e) {
return e.objectParameter > 5;
}).Any()
Lambdas are simply a syntax for expressing anonymous methods, which have existed in the language since .NET 2.0.
There isn't much difference between e => e.objectParameter > 5
, a lambda which takes an object e
and returns a boolean, and the older syntax, delegate(MyObj e) { return e.objectParameter > 5;}
. If you're using Resharper, you can use it to translate any lambda to an anonymous method and vice versa.
As such, lambdas aren't equivalent to LINQ, they're a shorthand syntax for anonymous functions. However, they're a stepping stone towards LINQ.
LINQ is, in its essence, a way to filter, transform and manipulate collections using techniques borrowed from functional programming. These techniques rely on the concept of Functions as First Class Objects, meaning you call a method (say, your Where
above) and pass it a function which determines how the Where
filters. Using C# before lambdas, this would have been very cumbersome, with the coder forced to use the verbose anonymous delegate syntax, or create a named method for each Where
call. (To see how clunky this can look, check out Java 7's anonymous types). So lambdas were added to the language to allow LINQ queries to be succinct, readable and (frankly) usable.