If you are using LinQ as ORM and getting your stuff done in a simple object-oriented way, that’s nice! Have you seen some one-liner magic code that doesn’t look very familiar to our usual C# code? Those are lambda expressions, an excellent way to get things done simply and easily. In today’s tutorial, We will discuss a basic understanding of C# lambda expression, which plays a vital role in. NET’s LinQ is a straightforward, efficient, and effective technology for handling 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
Code language: PHP (php)
Don’t get mad just by glancing at a lambda expression statement. It’s not an alien Language. It’s simply a Function(or delegate)! Well, we can call it an anonymous function. It’s a function without a name. But it has parameters and return values, which are dynamic, which means we can put anything for them; it’s 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, its scope finishes right there; there is no way to reuse it anymore, as there is no name assigned to it! So, we will mostly use this on some tasks that will require some computation, and we want to write it in an easy and efficient way, but we 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 expressions are handy while using LinQ queries in your application and applying them to a list of data, either from a database, XML, or dynamic. In other words, we usually use them to filter out some specific data from a list of data that meets the criteria and use them in a LinQ operation. Lambda expressions can be used in a wide range of areas, 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
Code language: PHP (php)
This is a very simple example of how a lambda expression can be. The first part, before the ‘=>’ symbol, is referred to as the input parameter. It doesn’t need to be defined previously; it’s 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. Let’s say we are dealing with a list of integer data; then x is an integer. If we are dealing with a list of ‘A’ objects, then x is also of ‘A’ type, and you will get full support of Visual Studio Intelligent for it.
So let’s see the complete statement of the above expression:
int[] somevalues = { 10, 20, 5, 2, 40, 1 };
int numberOfExpectedValues = somevalues.Count(x => x > 10);
Code language: JavaScript (javascript)
As you can see, the lambda expression returns values that are applied to the ‘Count’ method of the ‘somevalues’ array of integers. So, I mentioned now x will be treated as an integer. The expression will return a true/false value by testing each element of the ‘somevalues’ array and will be used as a delegate of the 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 can we use it on a list of objects, and how handy would that be? Let’s see a pseudo code in c#(you have to write a few more codes 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
Code language: PHP (php)
So, without using any ugly SQL query or a few lines of code for a function to do this manually, we have this in a single line of handy code. Isn’t it amazing?
References:
Check out the MSDN documentation on lambda expression as a further resource. I hope this small tutorial on linq c# lambda expression will help you further.
Please let me know if you are having any issues understanding any part of the tutorial. Keep in touch. Happy coding 🙂
Muhammed Shafi K says
Nice!!
Deva says
nice !!! thank u
Hummayun says
Good work… Jazakallah…
Mostafizur Rahman (@mostafiz57) says
Nice and clear description , Thanks @Rana.
ravi says
please change the font color of Pseudo Code