An Extensive Examination of LINQ: Lambda Expressions and Anonymous Types

Posted by: 4GuysFromRolla.com Headlines, on 11 Mar 2009 | View original | Bookmarked: 0 time(s)

The previous installment in this article series, Extension Methods, Implicitly Typed Variables, and Object Initializers, examined three new features to the C# 3.0 and Visual Basic 9 languages that allow for developers to use LINQ's standard query operators and providers to write SQL-like query syntax to work with common data stores. But extension methods, implicitly typed variables, and object initializers are only part of the story. Two additional language features - lambda expressions and anonymous types - are also essential ingredients in LINQ's unique syntax.

Recall that LINQ's standard query operators can work with any collection of data that implements the IEnumerable<T> interface. Some of the standard query operators perform rather straightforward operations on the collection; the Count standard query operator, for instance, simply returns the number of elements in the collection. However, other standard query operators are more flexible, allowing the page developer to dictate how the operator will work. The Where standard query operator is such an example - it filters the elements in the collection based on a developer-specified filtering method. But just how can a developer "pass" a method to the Where standard query operator as an input parameter? The .NET Framework has long supported the notion of delegates, which are type-safe function pointers. However, the syntax for delegates is a little confusing and verbose. Lambda expressions offer a much more terse syntax for defining an anonymous method.

Anonymous types allow for inline types. In a nutshell, anonymous types let you clump a set of values together into a new type without having to first declare the type via a class. For example, imagine that you have a collection of Employee objects, where each Employee object has properties like Name, Salary, HireDate, and so on. You may want to apply some filtering condition on this collection using the Where standard query operator and then project the collection of Employee objects into a collection of objects that includes just the Name and Salary properties. With anonymous types this style of projection is straightforward and natural. Without anonymous types you would first have to create a class like EmployeeSimplified that contained only those two properties of interest.

This article provides an overview of lambda expressions and anonymous types. These two language enhancements, along with extension methods, implicitly typed variables, and object initializers, are what allows for LINQ's query syntax. Read on to learn more!
Read More >

Advertisement
Free Agile Project Management Tool from Telerik
TeamPulse Community Edition helps your team effectively capture requirements, manage project plans, assign and track work, and most importantly, be continually connected with each other.
Category: XLinq | Other Posts: View all posts by this blogger | Report as irrelevant | View bloggers stats | Views: 1643 | Hits: 48

Similar Posts

  • An Extensive Examination of LINQ: Extension Methods, Implicitly Typed Variables, and Object Initializers more
  • Dynamic Queries and LINQ Expressions more
  • Speed Development Time by 50% more
  • Linq Data Services and the Business Logic Layer more
  • What data type should I use? more
  • LINQ's relationship to the new C#3 and VB9 features more
  • New "Orcas" Language Feature: Anonymous Types more
  • LINQ Resources more
  • Lambda expressions, Part II: Closures more
  • Demystifying C# 3.0 - Part 7: Expression Trees more

News Categories

.NET | Agile | Ajax | Architecture | ASP.NET | BizTalk | C# | Certification | Data | DataGrid | DataSet | Debugger | DotNetNuke | Events | GridView | IIS | Indigo | JavaScript | Mobile | Mono | Patterns and Practices | Performance | Podcast | Refactor | Regex | Security | Sharepoint | Silverlight | Smart Client Applications | Software | SQL | VB.NET | Visual Studio | W3 | WCF | WinFx | WPF | WSE | XAML | XLinq | XML | XSD