1.4. Lambda Expressions

Another new C# 3.0 feature is lambda expressions. This feature simplifies coding delegates and anonymous methods.

The argument to the Where<T> method we saw above is an example of a lambda expression:

Where(p => p.ID == 1)

Lambda expressions allow us to write functions that can be passed as arguments to methods, for example, to supply predicates for subsequent evaluation.

You could use code like that in Listing 1-4 to obtain the same result, but the lambda expression syntax is simpler.

Example 1-4. Alternative to Lambda Expression Syntax
Func<Person, bool> filter = delegate(Person p) { return p.ID == 1; };
var query = people
            .Where(filter)
            .Select(p => new { p.FirstName, p.LastName } );

ObjectDumper.Write(query);

Another advantage of lambda expressions is that they give you the ability to perform expression analysis using expression trees.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.15.17.154