Introducing LINQ—What Is LINQ
|Visual C# Tutorials|
|© 2006 J. Huddleston, et al|
What Is LINQ?
LINQ is a combination of namespaces and C# 3.0 (yes, we mean 3.0, not 2.0 or 2005) language enhancements. Through the very clever use of generics and other powerful new features of .NET 2.0 and using some functional programming techniques (like those natively available in F#), LINQ provides a high-level abstraction of virtually any data and emulates the query operations of the relational model. The LINQ Project seems to be just the beginning of many other future dramatic enhancements to .NET and .NET languages.
Note Throughout this book we’ve called the C# language C# 2005 because Visual C# 2005 is the name of the compiler Microsoft provides with .NET 2.0. Internally, Microsoft calls the language C# 2.0. Likewise, we’ve called SQL Server SQL Server 2005 because that’s the name of the product, though it’s internally version 9.0. Currently, the only name for the next version of C# is C# 3.0, so that’s why we’ve changed nomenclature.
These operations are coded using LINQ’s standard query operators (SQO), which are implemented as methods in class
Sequence in the
System.Query namespace. You can call the SQO methods directly, but C# 3.0 provides syntax that is much more elegant. You just code C# and the compiler transforms your code into the appropriate method calls.
Tip The source code for
System.Query.Sequence.cs is provided in the LINQ download.
LINQ has three major components:
- LINQ to Objects
- LINQ to ADO.NET, which includes
- LINQ to
DataSet(originally called LINQ over
- LINQ to Entities
- LINQ to SQL (originally called DLinq)
- LINQ to
- LINQ to XML (originally called XLinq)
LINQ to Objects deals with in-memory data. Any class that implements the
IEnumerable<T> interface (in the
System.Collections.Generic namespace) can be queried with SQO.
LINQ to ADO.NET deals with data from external sources, basically anything ADO.NET can connect to. Any class that implements
IQueryable<T> (in the
System.Query namespace) can be queried with SQO.
LINQ to XML is a comprehensive API for in-memory XML programming. Like the rest of LINQ, it includes SQO, and it can also be used in concert with LINQ to ADO.NET, but its primary purpose is to unify and simplify the kinds of things that disparate XML tools, like XQuery, XPath, and XSLT, are typically used to do. In this chapter we’ll preview LINQ to SQL and LINQ to DataSet, since they’re most closely related to the C# database programming we’ve covered in this book.
Note LINQ to Entities will bring LINQ to the ADO.NET Entity Framework, which combines an Entity Data Model with an extended version of SQL (eSQL) in yet another effort to address the data-object impedance issue. Since the Entity Framework is an ADO.NET 3.0 feature, we won’t cover LINQ to Entities here.