6 replies

Last post Mar 14, 2017 08:38 PM by Mikesdotnetting

• farooqspecia...

Member

46 Points

91 Posts

Good Resource to learn Lamba Expression

Is there any good resource to learn lambda expression with full methods used by the lamba expression?

• Rion William...

All-Star

113110 Points

18367 Posts

MVP

Re: Good Resource to learn Lamba Expression

Mar 14, 2017 02:21 PM|Rion Williams|LINK

Lambda Expressions

Lambda Expressions are basically anonymous functions that allow you to easily create delegates and map functions which can make querying databases or collections much easier. As seen in the example below :

`//An array of integers int[] integers = { 1 , 2, 3, 4, 5 }; //Using LINQ to grab all of the odd values var odds = integers.Where(i => i % 2 == 1);`

We have an array of integers 1-5 and we are going to use a lambda expression within the Where clause to only grab the odd values that occur in the collection. To do this, we will use the following lambda expression :

`i => i % 2 == 1`

Now what this does is actually create a mapping from i (which will represent each item within your collection) which will map to the function value on the other side of the "=>" which would yield the following results :

```1 : (1 % 2) == 1 //yields true
2 : (2 % 2) == 1 //yields false
3 : (3 % 2) == 1 //yields true
4 : (4 % 2) == 1 //yields false
5 : (5 % 2) == 1 //yields true```

so we can see that values 1, 3 and 5 result in true in our expression, which are in fact odd values.

The Where Clause will only return values that have an expression that results in true, which means that the following line of code would grab all of the odd values from your collection :

`var odds = integers.Where(i => i % 2 == 1); //yields { 1, 3, 5 }`

Ace provided an additional example that involved the use of complex objects, which is where you will most commonly encounter lambda expressions (commonly used within database queries).

101 LINQ Examples is a great place to start as it has tons of examples of different LINQ queries and will cover the code to help explain what is going on within the query. It starts out as very simple and progresses to more advanced queries.

A few other things to check out might be :

• raju dasa

All-Star

15176 Points

3889 Posts

Re: Good Resource to learn Lamba Expression

Mar 14, 2017 02:22 PM|raju dasa|LINK

Hi,

farooqspecials

Is there any good resource to learn lambda expression with full methods used by the lamba expression?

You can check this MSDN site:

https://msdn.microsoft.com/en-us/library/bb397687.aspx

Happy Coding ! Mark as Answer if it helps.
rajudasa.blogspot.com  ||  rajudasa-tech
• farooqspecia...

Member

46 Points

91 Posts

Re: Good Resource to learn Lamba Expression

What is the difference between lambda expressions and Linq .. Are these both same ?

• Rion William...

All-Star

113110 Points

18367 Posts

MVP

Re: Good Resource to learn Lamba Expression

Mar 14, 2017 04:37 PM|Rion Williams|LINK

farooqspecials

What is the difference between lambda expressions and Linq .. Are these both same ?

They go hand-in-hand, depending on your preferred syntax. Lambda queries are those that you'll generally see above that involve a mapping delegate (i.e. x => something), and can be used in such a way within LINQ queries.

LINQ itself however is a syntax that you can use to query a variety of things, such as database content, collections, etc. It isn't however restricted to the above "method" syntax, as the following could be used as well and it is technically LINQ without a lambda expression :

`var people = from p in PersonsTable select p.id;`

• PatriceSc

All-Star

40120 Points

12972 Posts

Re: Good Resource to learn Lamba Expression

The purpose of Linq is to be able to use C# as a "query language" (ie telling what you want rather than each step needed to get the result you want). It can be expressed as C# or using a specialized language that looks like SQL but which is still turned into C# code behind the scene.

A bit simplistic but you can think of lambda expressions as a way to pass code as arguments (as for any other data). This code can then run but also you can "inspect" it to see what it does. It is used in Linq to EF so that you can express your query in C# which is then turned into SQL which is used then to query the underlying database.

• Mikesdotnett...

All-Star

184748 Points

26712 Posts

Moderator

MVP

Re: Good Resource to learn Lamba Expression

farooqspecials

What is the difference between lambda expressions and Linq .. Are these both same ?

Linq chained methods make a lot if use of lambda expressions, but lambda expressions are also used elsewhere to represent a delegate. Both of the following are essentially the same, but the second one uses a lambda to represent the delegate:

```var result = Regex.Replace("elvis presley", @"\b[a-z]\w+", delegate(Match m)
{
string v = m.ToString();
return char.ToUpper(v[0]) + v.Substring(1);
});```
```var result = Regex.Replace("elvis presley", @"\b[a-z]\w+", m =>
{
string v = m.ToString();
return char.ToUpper(v[0]) + v.Substring(1);
});```

They both return "Elvis Presley", by the way.