Welcome!

Linux Authors: Michael Sheehan, Lavenya Dilip, Ian Thain, Bruce Armstrong, Ellen Rubin

Related Topics: .NET

.NET: Article

Book Review: LINQ in Action

Explains what LINQ does and why

Part II: LINQ to Object
There are several flavors of LINQ (LINQ-to-Objects, LINQ-to-SQL, LINQ-to-XML, and others will be added in the future). Part II gets the reader started in LINQ by introducing LINQ-to-Objects.

Chapter 4, the first of the two chapters in this section, covers the basics of LINQ, introduces the reader to the LINQ Books example that is used in the rest of the book, and shows how LINQ integrates with WinForms and ASP.NET.

Chapter 5 delves deeper into LINQ, covering design patterns to use with LINQ, how to get the best performance with LINQ, and other things you need to make the most of LINQ; my favorite part was a section on how LINQ can be used with .csv text files. I will use that at work Monday.

Part III: LINQ to Relational Data
Chapter 6 moves from querying objects to querying SQL databases, starting with using the System.Data.Linq.Mapping attributes to map objects and properties to tables and columns, and using the DataContext to connect to databases. The authors also show how to start debugging using the datacontext logging capability as well as how to use the Microsoft Query Visualizer, which is a separate download for Visual Studio 2008. Both of these methods allow you to see the SQL statements that will be generated by LINQ. Learning about free tools like this is worth a big portion of the book's price. The chapter continues with the basics of querying, and explains lazy loading and how it affects writing and testing LINQ code.

Chapter 7 goes into more detail about mapping objects to databases and shows four methods for doing the mapping. The first method uses the mapping attributes discussed in chapter 6, the second uses external XML files, and the final two use the command-line SQLMetal tool and the graphical LINQ-to-SQL Designer tool. The goal here is to show the reader how LINQ-to-SQL works "under the covers." This chapter also covers IQueryable and expression trees, the entity life cycle, and using disconnected data. This is the kind of information you get from people who work with a technology and then write a book about it, but not from someone who writes technology books about technology that they may know, but don't work with every day.

Chapter 8 covers advanced LINQ-to-SQL topics including concurrency issues, pass-through queries, and using stored procedures and user-defined functions with LINQ. It then covers improving business-tier code by making use of compiled queries, partial classes and methods, and inheritance. The chapter closes with an explanation of why the simple mapping between objects and tables ceases to work well as databases become even slightly complex, and how adding a conceptual layer between objects and database entries fixes the problem. The authors briefly discuss how the forthcoming LINQ-to-Entities will work.

Part IV: LINQ-to-XML
Chapter 9 shows why previous ways of working with XML data are difficult, and how the new LINQ-to-XML API avoids most of those problems. It then describes the LINQ-to-XML classes in the .NET Framework, including the "VB-only" XML Literals. Most of the chapter is dedicated to showing how to access and manipulate XML files.

Chapter 10 continues where chapter 9 leaves off, going into more detail about ways to query XML, including working with elements, attributes, descendants, ancestors, and elements/nodes before/after self. It then covers using the standard query operators, including grouping and ordering, with XML. The chapter closes with XPath and XML transformations.

Chapter 11 describes six common scenarios that use LINQ-to-XML, and then shows how to implement each of the scenarios. The scenarios are creating objects from XML, creating XML from objects, creating XML with data from databases, filtering and mixing data from databases with data from XML, reading XML and updating a database, and transforming text files into XML. Using any of these cookbook scenarios would recoup the price of the book.

Part V: "LINQ" It Together
Part V is divided into two chapters representing two different sections. Chapter 12 shows how to extend LINQ. It starts by showing how the different flavors of LINQ such as LINQ-to-Objects and LINQ-to-SQL are actually implementations of LINQ. It goes on to show how to create custom query operators, and ends by implementing a LINQ to Amazon flavor of LINQ. How cool is that?

The final chapter in the book shows how and why to use LINQ as a data access layer, and how to do it in ways that give the full benefit of an n tier application.

Summary
This is one of my favorite LINQ books. It was written by people who know, use, and care about LINQ, and who want to see it used correctly. It spends as much or more time explaining why LINQ works the way it does, as it spends explaining what LINQ does. The book also sneaks in some best practices explained by people who know, use, and understand LINQ, SQL, and database access. Most books I skim through for new bits of information, but this is one I want to read cover to cover, because there seems to be something to learn on every page.

More Stories By Dennis Hayes

Dennis Hayes is a programmer at Georgia Tech in Atlanta Georgia where he writes software for the Adult Cognition Lab in the Psychology Department. He has been involved with the Mono project for over six years, and has been writing the Monkey Business column for over five years.

Comments (0)

Share your thoughts on this story.

Add your comment
You must be signed in to add a comment. Sign-in | Register

In accordance with our Comment Policy, we encourage comments that are on topic, relevant and to-the-point. We will remove comments that include profanity, personal attacks, racial slurs, threats of violence, or other inappropriate material that violates our Terms and Conditions, and will block users who make repeated violations. We ask all readers to expect diversity of opinion and to treat one another with dignity and respect.