About This Book

Welcome to LINQ in Action. This book is an introduction to the Microsoft .NET LINQ technology and the rich toolset that comes with it.

LINQ stands for Language INtegrated Query. In a nutshell, it makes query operations like SQL statements into first-class citizens in .NET languages like C# and VB. LINQ offers built-in support for querying in-memory collections such as arrays or lists, XML, DataSets, and relational databases. But LINQ is extensible and can be used to query various data sources.

Our goal with this book is to help developers who have an existing knowledge of the .NET Framework and the C# or VB.NET language to discover the concepts introduced by LINQ and gain a complete understanding of how the technology works, as well as how to make the best of it in their projects.

LINQ in Action covers the entire LINQ spectrum. From Hello World code samples and the new C# 3.0 and VB.NET 9.0 features to LINQ’s extensibility and a tour of all the LINQ providers, this book has everything you need to get up to speed with LINQ and to be able to create applications that take advantage of it.

We believe this book provides the right mix of theory and examples. We made sure to keep the focus on the practical side of things, because we think that nothing’s better than a hands-on exploration of the technology. Several additions have been made to the C# and VB.NET languages, as well as to the .NET class library. But fear not: These language innovations aren’t difficult to grasp when you have the right code sample in front of you.

We’ll guide you along as you make your way through this new world where beasts like lambda expressions, query operators, and expression trees live. You’ll discover all the basics of LINQ that’ll help you form a clear understanding of the complete LINQ toolset. We’ll also provide a presentation of the common use cases for all the flavors of LINQ. Whether you want to use LINQ to query objects, XML documents, or relational databases, you’ll find all the information you’ll need. But we won’t stop at the basic code. We’ll also show you how LINQ can be used for advanced data processing. This includes coverage of LINQ’s extensibility, which allows us to query more data sources than those supported by default.

In order to base our code samples on concrete business classes, we’ll use a running example. This example, LinqBooks, is a personal book-cataloging system. This means that the LINQ queries you’ll see throughout the book will deal with objects such as Book, Publisher, and Author. The running example we’ve chosen is broad enough to involve all aspects of LINQ. We’ll progressively build the sample application throughout the chapters, finishing with a complete application in the last chapter.

Who should read this book

This book targets the .NET developer audience. Whether you don’t know much about LINQ yet or you already have a good knowledge of it, this book is for you.

In order to fully appreciate this book, you should already know C# or VB.NET, ideally C# 2.0 or VB.NET 8.0.

How the book is organized

This book has been written so that you can choose what you want to read and how you want to read it. It has 5 parts, 13 chapters, an appendix, a list of resources, and a bonus chapter.

Part 1 introduces LINQ and its toolset. It also helps you to write your first LINQ queries. If LINQ is new to you or if you want to make sure that you have all the background information required to correctly understand LINQ code, the first part of this book is for you. If you’re already familiar with LINQ and have a firm grasp on the new features of C# 3.0 and VB.NET 9.0, feel free to skip the first chapters and jump directly to other parts of the book that are related to specific uses of LINQ. If you want to understand where LINQ comes from, before you dive in, you may be interested in reading the bit of history we propose in chapter 1.

Part 2 is dedicated to LINQ to Objects and querying in-memory collections. This part also contains information about common LINQ use cases and best practices that’ll be useful when working with any LINQ flavor.

Part 3 focuses on LINQ to SQL. It addresses the persistence of objects into relational databases. It will also help you discover how to query SQL Server databases with LINQ. Advanced LINQ to SQL features are also presented, such as inheritance, transactions, stored procedures, and more.

Part 4 covers LINQ to XML. It demonstrates how to use LINQ for creating and processing XML documents. In this part, you’ll see what LINQ to XML has to offer compared to the other XML APIs. A comprehensive set of examples covers the most common LINQ to XML use cases.

Part 5 covers extensibility and shows how the LINQ flavors fit in a complete application. The extensibility chapter demonstrates various ways to enrich the LINQ toolset. The last chapter analyzes the use of LINQ in our running example and discusses choices you can make when you use LINQ.

The appendix contains a reference of the standard query operators, a key constituent of LINQ queries. Resources provides pointers to resources that will help you to learn more about LINQ, such as Microsoft’s official web sites, articles, weblogs or forums.

An online bonus chapter available as a download at http://www.manning.com/ LINQinAction and at http://LinqInAction.net introduces LINQ to DataSet. It demonstrates how LINQ can be used to query DataSets and DataTables.

It’s up to you to decide whether you want to read the book from start to finish or jump right into one precise chapter. Wherever you are in the book, we tried to make it easy for you to navigate between chapters.

Tools used

The LINQ technology is included in .NET 3.5. It is supported by Visual Studio 2008, C# 3.0, and VB.NET 9.0. All the content of this book and the code samples it contains are based on Visual Studio 2008 and .NET 3.5 RTM,[1] the final products. You can refer to section 1.4.1 to find a detailed list of software requirements for working with LINQ and this book’s samples.

1 Release To Manufacturing.

Source code

This book contains extensive source code examples in C# and VB.NET. All code examples can be found as a downloadable archive at the book’s web site at http://www.manning.com/LINQinAction and at http://LinqInAction.net. Not all the examples are provided in both C# and VB.NET at the same time in the book, but they’re all available in both languages in the companion source code.

Conventions

When we write “LINQ,” we’re referring to the LINQ technology or the complete LINQ framework. When we write “LINQ toolset,” we mean the set of tools LINQ offers: LINQ to Objects, LINQ to XML, LINQ to SQL, and the others. We’ll explicitly use LINQ to Objects, LINQ to XML, or LINQ to SQL to refer to specific parts of the LINQ toolset.

Typographical conventions

This book uses a special code font whenever certain code terms such as classes, objects, or operator names appear in the main text.

Particular bits of code that we want to draw attention to appear in bold. Furthermore, all code results and console output appears in italics.

Code annotations accompany many of the listings, highlighting important concepts. In some cases, numbered bullets link to explanations that follow the listing.

Icons like this differentiate between code in C# and VB.NET:

Author Online

Purchase of LINQ in Action includes free access to a private web forum run by Manning Publications where you can make comments about the book, ask technical questions, and receive help from the authors and from other users. To access the forum and subscribe to it, point your web browser to http://www.manning.com/ LINQinAction. This page provides information on how to get on the forum once you are registered, what kind of help is available, and the rules of conduct on the forum.

Manning’s commitment to our readers is to provide a venue where a meaningful dialogue between individual readers and between readers and the authors can take place. It is not a commitment to any specific amount of participation on the part of the authors, whose contribution to the book’s forum remains voluntary (and unpaid). We suggest you try asking the authors some challenging questions, lest their interest stray!

The Author Online forum and the archives of previous discussions will be accessible from the publisher’s website as long as the book is in print.

About the authors

FABRICE MARGUERIE is a software architect and developer with more than 13 years of experience in the software industry. He has diverse experience, ranging from consulting services and training to starting his own business. Fabrice has been awarded the C# MVP title by Microsoft in recognition for his involvement in the .NET community. His activities include speaking at conferences, writing technical articles in English and French, writing a weblog about .NET, and running websites such as sharptoolbox.com and proagora.com. Fabrice is based in Paris, France.

STEVE EICHERT is an architect and technical lead at Algorithmics, Inc. He also runs his own consulting company where he specializes in delivering solutions to clients utilizing the latest Microsoft .NET technologies. Steve can be found online at http://iqueryable.com. He is married and has three beautiful children. Steve is based in Philadelphia.

JIM WOOLEY has been working with .NET since PDC 2000 and has been actively evangelizing LINQ since its announcement in 2005. He leads the Atlanta VB Study Group and serves as INETA Membership Manager for the Georgia region.

About the title

By combining introductions, overviews, and how-to examples, the In Action books are designed to help learning and remembering. According to research in cognitive science, the things people remember are things they discover during self-motivated exploration.

Although no one at Manning is a cognitive scientist, we are convinced that for learning to become permanent it must pass through stages of exploration, play, and, interestingly, re-telling of what is being learned. People understand and remember new things, which is to say they master them, only after actively exploring them. Humans learn in action. An essential part of an In Action book is that it is example-driven. It encourages the reader to try things out, to play with new code, and explore new ideas.

There is another, more mundane, reason for the title of this book: our readers are busy. They use books to do a job or solve a problem. They need books that allow them to jump in and jump out easily and learn just what they want just when they want it. They need books that aid them in action. The books in this series are designed for such readers.

About the cover illustration

The caption for the figure on the cover of LINQ in Action reads “La Champenoise” or “The Champagne One.” The drawing is of a young woman from the historic province of Champagne in the northeast of France, best known for the production of the sparkling white wine that bears the region’s name. The illustration is taken from a French travel book, Encyclopedie des Voyages by J. G. St. Saveur, published in 1796. Travel for pleasure was a relatively new phenomenon at the time and travel guides such as this one were popular, introducing both the tourist as well as the armchair traveler to the inhabitants of other regions of the world, as well as to the and regional costumes and uniforms of French soldiers, civil servants, tradesmen, merchants, and peasants.

The diversity of the drawings in the Encyclopedie des Voyages speaks vividly of the uniqueness and individuality of the world’s towns and provinces just 200 years ago. This was a time when the dress codes of two regions separated by a few dozen miles identified people uniquely as belonging to one or the other. The travel guide brings to life a sense of isolation and distance of that period and of every other historic period except our own hyperkinetic present.

Dress codes have changed since then and the diversity by region, so rich at the time, has faded away. It is now often hard to tell the inhabitant of one continent from another. Perhaps, trying to view it optimistically, we have traded a cultural and visual diversity for a more varied personal life. Or a more varied and interesting intellectual and technical life.

We at Manning celebrate the inventiveness, the initiative, and the fun of the computer business with book covers based on the rich diversity of regional life two centuries ago brought back to life by the pictures from this travel guide.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.144.35.122