ow many software tasks DON'T involve querying data? Answer: very few.
Developers work all the time with database data, XML data, .NET DataSets, collections, lists, and arrays—and each requires a different syntax and has different functionality. Developers who write T-SQL code often covet set-based language statements when they work against other types of data. Additionally, developers who are used to coding against strongly-typed custom collections wish they could write SQL database queries with IntelliSense. Language Integrated Query (LINQ), a set of extensions to the .NET Framework for the next version of Visual Studio (codename "Orcas"), brings the promise of integrated and uniform query capabilities that should increase productivity when developers work with different types of data. While LINQ is a large topic—worthy of books—this edition of The Baker's Dozen provides a crash course so you can learn what's under the LINQ hood.
What Exactly Is LINQ?
|Author's Note: I created this article using Beta 1 for Visual Studio, codenamed "Orcas," which Microsoft released in late April 2007. For the remainder of this article I'll simply refer to it as "Orcas Beta 1."
Odds are high that just about every .NET developer has at least heard of Language Integrated Query (LINQ) by now. But because this is an intro, crash-course-style article on LINQ, I'll assume very little prior knowledge.
LINQ is a set of extensions to the .NET Framework to query different types of data using a common language. Orcas Beta 1 features the following LINQ capabilities within .NET:
- Querying Microsoft SQL Server data using a strongly-typed DataContext (LINQ to SQL)
- Querying in-memory .NET objects (LINQ to Objects)
- Querying .NET DataSets (LINQ to DataSets)
- Querying XML Data (LINQ to XML)
LINQ offers something for just about everyone. If you're writing stored procedures and don't need to change your approach, you may still find value in LINQ to DataSets or LINQ to Objects.
This article presents a number of code samples to demonstrate LINQ's functionality. In addition, I'll also cover new language enhancements in the Orcas Beta 1 release that work in conjunction with LINQ to make developers more productive with data-centric tasks than ever before. As of this writing, LINQ has gone from the Community Technology Preview (CTP) phase to Beta 1, so the odds are high that functionality will change. But the code samples should give you a good foundation for learning LINQ.
What's on the Menu?
In creating the tips for this article, I wanted to provide basic exposure to as many areas of LINQ as possible. As I often do with intro articles and presentations, I try to build the types of examples that I previously sought when I learned a particular technology. So here are the 13 tips, in hopes of making you more aware of what's under the LINQ hood, and LINQ's uses:
|Lambda expressions are a "kinder, gentler" form of anonymous methods.|
- Installing Beta 1 and getting started
- First steps for using LINQ to SQL by creating a strongly-typed DataContext
- Writing LINQ to SQL queries
- Standard LINQ querying syntax and operators, such as sorting, string searching, date handling, and SELECT DISTINCT
- More querying syntax and operators, such as UNION and EXISTS, as well as SKIP/TAKE for paging and ANY/ALL condition handling
- Using an extension method to convert a LINQ result set to an ADO.NET DataTable
- Converting a LINQ result set to an IEnumerable or to a List
- LINQ to XML
- LINQ to DataSets
- Lambda Expressions
- LINQ to Objects
- Language features for C# 3.0
- Language features for VB 9.0
Additionally, you'll find several valuable LINQ online references at the end of the article.
No ADO.NET Entity Framework—Yet
Microsoft's data access strategy in .NET consists of two major components: one is LINQ, which this article covers. The other is the ADO.NET Entity Framework, a significant upgrade to ADO.NET that allows developers to create data models of the back-end database.
As I write this article (late April 2007), Microsoft has announced that the ADO.NET Entity Framework (which I'll call Entity Framework throughout the remainder of this article) will not ship with Orcas Beta 1—but will ship a few months later. The Entity Framework promises a richer development experience for creating and mapping entity data models to relational databases. You can read more about the Entity Framework here.
Given that Microsoft will not release the Entity Framework until after Orcas Beta 1 ships, I've decided to focus on areas of LINQ that are scheduled to be part of this release. In a future article I'll cover the Entity Framework and Microsoft's data access strategy.
|Editor's Note: This article was first published in the July/August 2007 issue of CoDe Magazine, and is reprinted here by permission.|