The Baker’s Dozen: A 13-Step Crash Course for Using LINQ How many software tasks DON’T involve reading through data? Answer: very few. Developers work all the time with database data, XML data, DataSets, collections, lists, and arrays-all with different syntax and functionality for each one. Developers who write T-SQL code often covet set-based language statements when they work against other types of data. Additionally, developers who have coded 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 to increase developer productivity when working with different types of data. While LINQ is a large topic worthy of books, this edition of The Baker’s Dozen will provide a crash course to learn what’s under the LINQ hood. So what Exactly Is LINQ? Odds are high that just about every .NET developer has at least heard of Language Integrated Query (LINQ). But since this is an intro, crash-course-style article on LINQ, I’ll assume very little. | " | Lambda expressions are a “kinder, gentler” form of anonymous methods.
| " |
LINQ is a set of extensions to the .NET Framework to query different types of data using a common language. The next release of Visual Studio (codenamed “Orcas”) will feature 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 database 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 will present a number of code samples to demonstrate LINQ functionality. In addition, I’ll also cover new language enhancements in Visual Studio codename “Orcas” that work in conjunction with LINQ to allow developers to work with data more productively than ever before. As of this writing, LINQ has gone from the Community Technology Preview (CTP) phase to Beta 1, so 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 what you can use it for. - Installing the beta 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, at the end of the article I’ll list several valuable LINQ online references. Let’s get started with LINQ! 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 Visual Studio codename “Orcas”, 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: http://blogs.msdn.com/data/archive/2007/04/28/microsoft-s-data-access-strategy.aspx Given that Microsoft will not release the Entity Framework until after Visual Studio codename “Orcas” ships, I’ve decided to focus on areas of LINQ that are scheduled to be part of the Visual Studio codename “Orcas” release. In a future article I’ll cover the Entity Framework and Microsoft’s data access strategy. | & | | 
By: Kevin S Goff
Kevin S. Goff, a Microsoft MVP award recipient for 2007, is the founder and principal consultant of Common Ground Solutions, a consulting group that provides custom Web and desktop software solutions in .NET, VFP, SQL Server, and Crystal Reports. Kevin is the author of Pro VS 2005 Reporting using SQL Server and Crystal Reports, published by Apress. Kevin has been building software applications since 1988. He has received several awards from the U.S. Department of Agriculture for systems automation. He has also received special citations from Fortune 500 Companies for solutions that yielded six-figure returns on investment. He has worked in such industries as insurance, accounting, public health, real estate, publishing, advertising, manufacturing, finance, consumer packaged goods, and trade promotion. In addition, Kevin provides many forms of custom training. Contact Kevin at kgoff@commongroundsolutions.net
kgoff@commongroundsolutions.net | Fast Facts | | This article demonstrates the different areas of LINQ (LINQ to SQL LINQ to Objects, LINQ to XML, LINQ to DataSets), using the first official Beta for Microsoft Visual Studio codename “Orcas”. | |
|