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 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.
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.
In some cases LINQ is just as fast if not faster than other methods, but in other cases it can be slower. We work on a project that we converted to linq and the data lookup is faster but the merging of data between two tables is much slower.
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.
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.
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)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With