What is Lambda-Expressions in C#?

Tram Ho

Lambda-Expressions in C#

The language I approached in the first style of interest was Javascript, one day I became acquainted with C # and found it also had the “=>” mark like in the Arrow function in the JS so I started going to find Understand this guy how to use it 😆😆

It can be said that compared to the anonymous method, the lambda expression is considered a worthwhile improvement from version C # 2.0 to C # 3.0. When using anonymous methods, you can create in-line functions to limit the declaration of unnecessary individual functions, making the code shorter. Now with lambda expression, you can write shorter and more easily by providing new operators and syntax, and show the “intelligence” of the compiler by identifying the type of the data itself.

1. What is Lambda Expressions

Lamda Expression is an unamed function with parameters and body contents. The content of Lamda expression can be 1 block or 1 expression. The mark “=>” separates the parameters and the execution content, I find it quite similar to the Arrow function in JS

2) Apply the Lambda Expressions syntax

1. It is possible to omit the data type of the parameter passed

=> The above two ways of writing are equivalent, the compiler automatically identifies the type of data and executes 2. If there is no parameter, leave the () sign blank

=> This is required to distinguish the parameter, if you omit the “()” sign without parameters then the program will error

3. If there is only 1 parameter, you can remove the trailing sign ()

=> 2 The above syntax is equivalent, remember that only 1 parameter we can use like that, if there are 2 parameters that you do not put inside the “()” sign, the program will give an error

4. If there are multiple parameters, separate them with commas

5. If the anonymous function has only 1 statement, you can remove the “{}”

=> The above 2 syntaxes are equivalent, only applies to the execution of 1 statement , if there is more than 1 command, you must leave them in the “{}”

6. If return only 1 value, you can remove the word return

Overview of shortening 1 lambda expressions:

To find out more, I will go into a specific example with ASP .NET below 👇

3. Example lambda expressions with ASP .NET

3.1. Create an ASP .NET MVC Project

3.2. Create Model

  • Create a Model named Book in the Models folder and configure the following

3.3. Create a Context and connect to the database

  • Open SQL server and create a database LibraryDB:
  • Install the Entity Framework nuget package into the project:
  • Create a Context folder in the project and create a BookContext.cs file to configure the data connection context
  • Configure BookContext.cs as follows

3.4. Create Controller and View

  • Create the BookController.cs controller in the Controllers directory, configure the following:

  • Proceed to the Rebuild Project , and right click on the Action Index area and select add View:
  • Configure the View as follows:
  • Add the following ActionLink to /View/Book/Index.cshtml just created:
  • Create view for GET Book / Create method in BookControlle.cs:
  • Configure the Action Create for the POST Book / Create method in BookControlle.cs:
  • Now Run App and add some data to use it ✌️

3.5. Use Lambda Expressions and LINQ to filter data

  • If you do not have any knowledge of LINQ, you can refer to the article about LinQ in your .NET framework .
  • Return to BookController and add ActionResult ShowAfter1990 as follows:
  • Conduct Add View for this Action:
  • Run App => And as a result, we have used the filter function for books released after 1990:
  • And the function of Action ShowAfter2000 you try to do the same yourself 😉

3.6. Explain the example

Above I used the Lambda syntax:

The explicit of this query is:

Very verbose than using a lambda expression, right 😀😀

4. Summary

  • In my opinion, lambda expression is a good improvement or upgrade. It helps us to write short code but still easy to understand, and easy to learn.
  • Moreover, because most programmers prefer to see symbols and operators because they are pictographs instead of dry words and words, surely this will make an interesting syntax. 🤗
Share the news now

Source : Viblo