advertisement
Lambda Expression in C#
Lambda Expressions
A method associated with a delegate is never invoked by itself, instead, it is only invoked through the delegate. Sometimes, it can be very cumbersome to create a separate method so it can be invoked through the delegate. To overcome this, anonymous methods and lambda expressions were used. Anonymous methods allow unnamed blocks of code to be created to represent a method referred by a delegate. A lambda expression is an anonymous expression that can contain expressions and statements and enables simplification of development through inline coding. In simple terms, a lambda expression is an inline expression or statement block having a compact syntax and can be used wherever a delegate or anonymous method is expected.
Syntax
parameter-1ist => expression Or statements
where,
- parameter-list: This is an explicitly typed or implicitly typed parameter list.
- =>: Is the lambda operator.
- expression or statements: These are either an expression or one or more statements.
For example, the following code is a lambda expression:
word > word. Length;
Consider a complete example to illustrate the use of lambda expressions. Assume that a developer wants to calculate the square of an integer number. The developer can use the method Square () and pass the method name as a parameter to the Console. WriteLine () method.
1. Expression Lambdas
An expression lambda is a lambda with an expression on the right side. It has the following syntax:
Syntax
(input parameters) => expression
where,
- input_parameters: One or more input parameters, each separated by a comma
- expression: The expression to be evaluated
The input parameters may be implicitly typed or explicitly typed.
When there are two or more input parameters on the left side of the lambda operator, they must be enclosed within parentheses. If there is no parameter at all, a pair of empty parentheses must be used. However, if only one input parameter and its type are implicitly known, then the parentheses can be omitted.
Consider the lambda expression,
(str, str1) => str == str1
It means str and str1 go into the comparison expression which compares str with str1. In simple terms, it means that the parameters str and str1 will be passed to the expression str == str1.
It is not clear what are the types of str and str1, Hence, it is best to explicitly mention their data types:
(string str, string str1) => str==str1
To use a lambda expression, declare a delegate type that is compatible with the lambda expression. Then, create an instance of the delegate and assign the lambda expression to it. After this, the developer will invoke the delegate instance with parameters, if any. This will result in the lambda expression being executed. The value of the expression will be the result returned by the lambda.
2. Statement Lambdas
A statement lambda is a lambda with one or more statements. It can include loops, if statements, and so forth.
(input parameters) =>statement ;
where,
- input parameters: One or more input parameters, each separated by a comma
- statement: A statement body containing one or more statements
Optionally, a developer can specify a return statement to get the result of a lambda.
(string str, string str1) => return (str==str1) ;}
3. Lambdas with Standard Query Operators
Lambda expressions can also be used with standard query operators. lists the standard query operators.
- Sum: Calculates the sum of the elements in the expression
- Count: Counts the number of elements in the expression
- OrderBy: Sorts the elements in the expression
- Contains: Determines if a given value is present in the expression
================
Query Expressions
A query IS a set of instructions that retrieves data from a data source. The source may be a database table, an ADO.NET dataset table, an XML file, or even a collection of objects such as a list of strings. A query expression is a query that is written in query syntax using clauses such as from, select, and so forth. These clauses are an inherent part of a LINQ query. LINQ is a set of technologies introduced in Visual Studio 2008. It simplifies working with data present in various formats in different data sources. LINQ provides a consistent model to work with such data.
Through LINQ, developers can now work with queries as part of the C# language. Developers can create and use query expressions, which are used to query and transform data from a data source supported by LINQ. A from clause must be used to start a query expression and a select or group clause must be used to end the query expression.
Some of the commonly used query keywords seen in query expressions are listed.
- from: Used to indicate a data source and a range variable.
- where: Used to filter source elements based on one or more boolean expressions that may be separated by the operators && or ||.
- select: Used to indicate how the elements in the returned sequence will look like when the query is executed
- group: Used to group query results based on a specified key value
- order by: Used to sort query results in ascending or descending order
- ascending: Used in an orderby clause to represent ascending order of sort
- descending: Used in an orderby clause to represent descending order of sort
advertisement
Conversation
Your input fuels progress! Share your tips or experiences on prioritizing mental wellness at work. Let's inspire change together!
Join the discussion and share your insights now!
Comments 0