Understanding LINQ (C#) – An article describing the new language features of LINQ
You can read this article on CodePoject on http://www.codeproject.com/KB/linq/UnderstandingLINQ.aspx
- New Language Features
- Automatic Properties
- Local Variable Type Inference
- Object Initializers & Collection Initializers
- Anonymous Types
- Lambda Expressions
- Extension Methods
- Query Syntax
- Final Notes
- References & Resources
This article is about LINQ which I think is one of the most exciting features in Orcas. LINQ makes the concept of querying a first-class programming concept in .NET. The data to be queried can take the form of XML (LINQ to XML), databases (LINQ-enabled ADO.NET: LINQ to SQL, LINQ to Dataset and LINQ to Entities) and objects (LINQ to Objects). LINQ is also highly extensible and allows you to build custom LINQ enabled data providers (e.g.: LINQ to Amazon, LINQ to NHibernate, LINQ to LDAP).
I will discuss some of the new language features and improvements that are introduced in C# 3.0, and it is those features that enable the full power of LINQ which makes it possible to write something like this:
Remember that if you want to play around with LINQ or try the examples yourself, you will need to download Visual Studio Orcas Beta 1.
In case you don’t want to download the whole VS, you can check the LINQ Preview (May 2006 CTP)
which runs on top of VS 2005 (there are a few changes in Beta 1 from the way
LINQ worked in the May CTP).
The above code simply defining a class with basic properties. Now with the new C# compiler in Orcas, we can write a shorter cleaner version using Automatic Properties which automatically generates the private fields with get/set operations :
The code above is even more readable and less verbose.
Note that this feature has nothing to do with LINQ, I just thought it would be appropriate to list it with the other new language features.
With this feature, the type of the local variable being declared is inferred from the expression used to initialize the variable. This is achieved using the var keyword (familiar to those who works with scripting language but actually is quite different). It allows us to write the following code:
The compiler would generate the same IL as if we compiled:
Note that there is no un-typed variable reference nor late-binding happening, instead the compiler is inferring and declaring the type of the variable from the right-hand side of the assignment, thus the var keyword is generating a strongly typed variable reference.
Lets use the same Point class defined earlier, an suppose we want to define an instance of this class. We will have to create the object and start setting its properties, the code would look like this:
This could be rewritten using Objects Initializers and combined into:
This feature can also be used with collection. Take a look at this example:
Note that the compiler will generate a long hand code equivalent to the above one. It makes calls to the Add() method to add elements to the collection one at a time.
This language feature enable us to define inline types without having to explicitly define a class declaration for this type. In other words, imagine we want to use a Point object without defining the class Point (it would be anonymous). We will use the same object initializer syntax discussed earlier but without the type name:
Inside Orcas you will have full intellisense support. So when you use the variable p you will get a list of properties this anonymous type has.
C# 2.0 introduced anonymous methods, which allow code blocks to be written “in-line” where delegate values are expected. While anonymous methods provide the power of functional programming languages, the syntax is rather verbose. Lambda expressions provide a more concise, functional syntax for writing anonymous methods. A lambda expression is written as a parameter list (can be implicitly typed), followed by the => token, followed by an expression or a statement block.
As an example lets define a delegate type MyDeleg as:
we can then write using anonymous methods:
Or we can use the new lambda expressions to write:
Extension methods make it possible to extend existing types and constructed types with additional methods, without having to derive from it or recompile the original type. So instead of writing helper methods for objects they become part of that object itself.
As an example, suppose we want to check a string to see if it is a valid email address. We would do this by writing a function that takes a string as an argument and returns a true/false. With Extension Methods we can do the following:
We defined a static class with a static method containing the extension method. Note how the static method above has a this keyword before the first parameter argument of type string. This tells the compiler that this particular Extension Method should be added to objects of type string. And then we can call it from the string as a member function:
It is worth mentioning that LINQ syntax makes use of built-in Extension Methods (e.g.: where(), orderby(), select(), sum(), average() and many more) that reside in the new System.Linq namespace in Orcas and define standard query operators that can be used against relational databases, XML and any .NET objects that implement IEnumerable<T>.
Query expressions provide a language integrated syntax for queries that is similar to relational and hierarchical query languages such as SQL and XQuery. It is a shorthand for writing queries using the LINQ query operators (i.e. from…where…select). Visual Studio provides full intellisense and compile-time checking support for query syntax.
When the C# compiler encounters a query syntax expression it actually transforms it into into explicit method invocation code that uses Extension Methods and Lambda Expressions.
To explain this, let me give an example:
The above code is equivalent to the following:
The advantage of using Query Syntax is that the code is easier and more readable.
Also note that a query expression begins with a from clause and ends with either a select or group clause.
Most of the language features introduced in C# v3.0 that we saw (such as variable type inference, object initializers, anonymous types and lambda expressions) are only “compiler tricks”/”syntactic sugar” that means that the IL the compiler generates is the same as the long code version, therefore they are independent from the framework (NetFX) and runtime (CLR). However, they do need some framework support and specifically the “green bits” (.NET Framework v3.5) System.Core.dll assembly. This is why extension methods which in fact works at compile time (syntactic sugar) still rely on System.Runtime.CompilerServices.ExtensionAttribute introduced in System.Core.dll
On the other hand, query expressions syntax is just a mapping to the extension methods implementations that resides in System.Linq, System.Data.Linq and System.Xml.Linq namespaces.
- “New Orcas Language Feature” Series by Scott Guthrie: Great series explaining LINQ
- LINQ Resources: Blog entries by Daniel Moth
- MSDN Magazine (June 2007): An article on C# 3.0 and LINQ
- Visual Studio Orcas Samples: C#/VB LINQ Samples for Beta 1
- Future Versions: C# 3.0
- The LINQ Project: Home of the LINQ Project