Blue Theme Orange Theme Green Theme Red Theme
 
Nevron Chart
Home | Forums | Videos | Photos | Blogs | Beginners | Advertise with Us
 | Consulting  
Submit an Article Submit a Blog 
Search :       Advanced Search »
Home » Blogs Home » Blog Detail

Lambda Expressions and Expression Trees

 by Dinesh Beniwal on Oct 08, 2009

Many query operators allow the user to provide a function that performs filtering, projection, or key extraction.
Comments: 0 Views: 6943 Printable Version 

 

Many query operators allow the user to provide a function that performs filtering, projection, or key extraction. The query facilities build on the concept of lambda expressions, which provide developers with a convenient way to write functions that can be passed as arguments for subsequent evaluation. Lambda expressions are similar to CLR delegates and must adhere to a method signature defined by a delegate type. To illustrate this, we can expand the statement above into an equivalent but more explicit form using the Func delegate type:

Func<string, bool>   filter  = s => s.Length == 5;
Func<string, string> extract = s => s;
Func<string, string> project = s => s.ToUpper();

IEnumerable<string> query = names.Where(filter)
                                 .OrderBy(extract)
                                 .Select(project);

Lambda expressions are the natural evolution of anonymous methods in C# 2.0. For example, we could have written the previous example using anonymous methods like this:

Func<string, bool>   filter  = delegate (string s) {
                                   return s.Length == 5;
                               };

Func<string, string> extract = delegate (string s) {
                                   return s;
                               };

Func<string, string> project = delegate (string s) {
                                   return s.ToUpper();
                               };

IEnumerable<string> query = names.Where(filter)
                                 .OrderBy(extract)
                                 .Select(project);

Comment Request!
Thank you for reading this post. Please post your feedback, question, or comments about this post Here.
 
What do you say about this post? Post a comment here
*Title:
*Comment:
 
Comments not available.
6 Months Free & No Setup Fees ASP.NET Hosting!
Become a Sponsor

 Blogger's Profile
Age: Not Available
Location:
Title: Tech Lead
Joined: Nov 04, 2008
Education: Masters Degree
 More Blogs from this Blogger
Initializing Compound Values
Deferred Query Evaluation
Extension Methods
Lambda Expressions and Expression Trees
Getting Started with Standard Query Operators
.NET Language-Integrated Query
Window Resizing
The Layout System in Silverlight
The Silverlight Plug-in Position and Dimensions
Colors and Brushes for Text Foreground
View all »
 Latest Blogs
by willInstyler Rotating Iron they get discount
Initializing Compound Values
Deferred Query Evaluation
Extension Methods
Lambda Expressions and Expression Trees
Getting Started with Standard Query Operators
.NET Language-Integrated Query
Window Resizing
The Layout System in Silverlight
The Silverlight Plug-in Position and Dimensions
View all »
 Latest Jobs
More ... 
 Latest Interview Questions
More ... 
Mindcracker MVP Summit 2012
 © 2012  contents copyright of their authors. Rest everything copyright Mindcracker. All rights reserved.