
If you are using LinQ as ORM and getting your staffs done with simple object oriented way, that’s nice! Have you seen some one liner magic code which don’t look much familiar to our usual C# code? Those are lambda expressions, a cool way to get things done in simple and easy way. In today’s tutorial We will discuss about basic understanding of C# lambda expression, which play a very important role in .NET’s LinQ, a very easy,efficient and effective technology to handle data.
If you are very new and don’t have any knowledge on linq yet, you may consider reading my other post about beginners guide to linq
What Is Lambda Expressions?
(Input Parameters) => Method Expression
don’t get mad just by having a glance at a lambda expression statement, It’s not an alien Language. It’s simply a Function(or delegate)! Well, we can call it as anonymous function. It’s a function without a name. But it has parameter and return value, but they are dynamic, which means we can put anything for them, its on us. Wow! How flexible. Yes, it is.
However, you will have to remember one thing though, as soon as we use it in code, wherever that is, it’s scope finishes right there, no way to reuse it anymore, as there is to name assigned to it! So, mostly, we will use this on some tasks which will need some computation and we want to write it easy and efficient way, but don’t need to reuse it again often. If we need so, this must be assigned to a delegate or ‘Func’ type.
General Usage Of C# Lambda Expression:
In general lambda expression is handy while using LinQ query in your application an apply them on a list of data, either from database, from XML or dynamic. In other words, we usual use it to filter out some specific data from a list of data that meets the criteria and use them on a LinQ operation.lambda expression can be used in wide range of area like linq to sql, linq to xml, linq to entity(Entity framework), array/list data etc.
Read All LinQ Tutorials By CodeSamplez.com
A Simple Example:
Please take a look at the following expression:
x => x >10
This is a very simple example how a lambda expression can be. The first part, before the ‘=>’ symbol, is refereed as the input parameter. It doesn’t need to be defined previously, its dynamic. It decides the type intelligently, even in compile time, when you are writing it, based on the operation being performed on it.
As we said, we want to filter out some specific data from a list of data, this input parameter stands for the type of each element of the list. Lets say, we are dealing with a list of integers data, then x is an integer. If we are dealing with a list of ‘A’ object, then x is also be of ‘A’ type and you will get full support of visual studio intelligent for it.
So lets see the complete statement of the above expression:
int[] somevalues = { 10, 20, 5, 2, 40, 1 }; int numberOfExpectedValues = somevalues.Count(x => x >10);
As you can see, the lambda expression is returning value which are being applied on ‘Count’ method of ‘somevalues’ array of integers. So, I mentioned, now x will be treated as an integer. And the expression will return a true/false value with testing each element of ‘somevalues’ array and will be used as delegate of count method. finally, in the ‘numberOfExpectedValues’ variable we will receive how many data meet the criteria of being greater than 10.
Another Pseudo Code Example:
How we can use it on a list of objects and how handy that would be? Lets see a pseudo code in c#(you have to write few more code to make it compile):
List<User> myUsers = New List<User>(); //fill the list myUsers with some User object List<User> activeUsers = myUsers.Where(u => u.Active == True); //the activeUsers list will contain only active users filtered from list of all users
So, without using any ugly SQL query or few lines of codes for a function to do this manually, we are having this in a single line of handy code. Isn’t it amazing?
References:
Checkout msdn documentation on labmda expression as further resource. Hope this small tutorial on linq c# lambda expression will help you in your way further.
Please let me know if you are having any issue in understanding any part of the tutorial. Keep in touch. Happy coding 🙂
Nice!!
nice !!! thank u
Good work… Jazakallah…
Nice and clear description , Thanks @Rana.
please change the font color of Pseudo Code