Tuesday, October 5, 2010

Lambda Expressions

 
Lambda Expressions
A lambda expression is a convenient means to assign an expression or block of code (an anonymous
method) to a variable (a delegate). A lambda expression accomplishes this with an absolute minimum
amount of typing. Although VB 9.0 doesn ’ t offer anonymous methods, it does support lambda
expressions, which members of Microsoft ’ s VB team also call inline methods . Without support for lambda
methods VB developers couldn ’ t take advantage of LINQ.
Here ’ s the basic syntax for lambda expressions:
argument-list = > expression-or-statement-block // C# 3.0
Function(argument-list) expression ‘ VB 9.0
Lambda expressions extend anonymous methods with the following capabilities:
Parameter type inference, which eliminates the need to specify parameter types
Conversion to expression trees, which treat code as data
Pass as method arguments, implementing argument type inference and method overload
resolution
Substitution of C# statement blocks for expressions, where necessary
VB 9.0 lambda expressions don ’ t support statement blocks.
Lambda expressions implement the method call syntax form of LINQ ’ s Standard Query Operators
(SQO), which are the subject of the later “ Standard Query Operators ” section and Chapter 3 and
Chapter 4 .
C# 3.0 Lambda Expressions
Moving from the anonymous method to lambda expression syntax is a stepwise process. Here are the
steps using the sample delegate(LineItem i) { return i.UnitPrice > = 25M; } anonymous
method from the last (C#) example of the earlier “ Anonymous Methods and Generic Predicates ” section:
1. Delete the delegate keyword, leaving (LineItem i) { return i.UnitPrice > = 25M; } .
2. Replace the French braces with the lambda operator ( “ fat arrow ” ), leaving (LineItem i) = >
return i.UnitPrice > = 25M; .
3. Remove the return keyword and delete the semicolon because i.UnitPrice > = 25M has
become an expression, leaving (LineItem i) = > i.UnitPrice > = 25M .
4. The compiler can infer the type, so remove LineItem and the parentheses, which leaves i = >
i.UnitPrice > = 25M , an operable lambda expression to supply the predicate.
The lengthy predicates test code that uses an anonymous function becomes:
C# 3.0
// Predicates test (lambda function)
var anon = LineItemsList.Find( i = > i.UnitPrice > = 25M );
The Find() method of the preceding example is called a singleton method because it returns a single
LineItem type, not an IEnumerable < T > type.
Most folks pronounce the lambda operator ( = > ) “ goes to ” but some prefer “ such that ” or “ begets. ”
Generic List < T > types support IEnumerable < T > and foreach / ForEach iteration. The following code,
which resembles some of the simple LINQ queries you saw in Chapter 1 , adds two LineItemsList
rows and a count of LineItem s with UnitPrice values > = $20.00.
C# 3.0
// Predicates test (lambda function, multiple values)
result = “\r\nPredicate(Of T) Test (Lambda Function, Multiple Items) \r\n”;
var mult = LineItemsList.FindAll(i = > i.UnitPrice > = 20M);
foreach (var i in mult)
result += i.OrderID.ToString() + “\t” + i.ProductID.ToString() + “\t\t” +
i.UnitPrice.ToString() + “\r\n”;
txtResult.Text += result;
The FindAll() method returns an IEnumberable < T > type to enable iteration with the foreach
operator. The FindAll() method isn ’ t invoked against the LineItemsList collection until execution
reaches the foreach operator, which executes yield return.
Another capability of lambda expressions is composability by chaining extension methods. Here ’ s a
simple example that returns the number of items with a UnitPrice value > = $20 :
C# 3.0
var quan = LineItemsList.FindAll(i = > i.UnitPrice > = 20M).Count;
txtResult.Text += quan.ToString() + “ Items with Price > = $20.00\r\n”;
You can chain multiple lambda expressions that return IEnumerable < T > types to apply complex
filtering and sorting operations to arrays and List < T > types. The following “ Standard Query
Operators ” section provides examples of chained lambda expressions.

REFERENCE : Wrox.Professional.ADO.NET.3.5.with.LINQ.and.the.Entity.Framework

No comments:

Post a Comment