Tải bản đầy đủ (.pdf) (624 trang)

Pro LINQ Language Integrated Query in C Sharp 2008 docx

Bạn đang xem bản rút gọn của tài liệu. Xem và tải ngay bản đầy đủ của tài liệu tại đây (9.77 MB, 624 trang )

this print for content only—size & color not accurate spine = 1.176" 624 page count
Books for professionals By professionals
®
Pro LINQ: Language Integrated Query in C# 2008
Dear Reader,
Pro LINQ: Language Integrated Query in C# 2008 is all about code. Literally,
this book starts with code and ends with code. In writing this book, it has been
my desire to create a treasury of meaningful LINQ examples. Rather than show
you a single, simplest case example, I’ve strived to fill in the whole picture and
demonstrate the breadth of LINQ operators and prototypes that are available
to you. With this information, you will be able to put LINQ to use as it was
intended and reap the maximum rewards for your investment.
Throughout this book, it is my aim to give you the information that actually
matters in a form that you can use. So, rather than obscure the relevant LINQ
principles by focusing on a complex demonstration application you can’t put to
practical use, Pro LINQ cuts right to the chase of each LINQ operator, method,
or class. However, where complexity is necessary to truly demonstrate an issue,
the examples are right there in the thick of it. For example, code samples dem-
onstrating how to handle concurrency conflicts actually create concurrency
conflicts, so you can step through the code and see them unfold.
This book is for anyone with an elementary understanding of C# who wants
to understand LINQ and LINQ-relevant C# 3.0 language features. You need not
be up on all the latest C# 2.0 or 3.0 features to understand Pro LINQ. When a
deeper knowledge of an advanced language feature is necessary, I begin from
the ground up to make sure everyone is well equipped for the discussion.
Joseph C. Rattz, Jr.
US $44.99
Shelve in
Programming/C#
User level:
Intermediate–Advanced


Rattz
LINQ
The eXperT’s Voice
®
in .neT
Pro
LINQ
Language Integrated Query
in C# 2008
cyan
MaGenTa
yelloW
Black
panTone 123 c
Joseph C. Rattz, Jr.
Companion
eBook Available
THE APRESS ROADMAP
Silverlight and .NET 3.5
Recipes in C# 2008
Pro C# 2008 and the
.NET 3.5 Platform
Pro WPF in C# 2008, 2e
Illustrated C# 2008
Accelerated C# 2008
Pro .NET 3.5 Scalable
Application Design
Expert Service-Oriented
Architecture, 3e
Beginning ASP.NET 3.5

Data Access, 2e
Beginning
C# 2008 Databases
Beginning C# 2008
Pro LINQ: Language
Integrated Query
in C# 2008
www.apress.com
SOURCE CODE ONLINE
Companion eBook

See last page for details
on $10 eBook version
ISBN-13: 978-1-59059-789-7
ISBN-10: 1-59059-789-3
9 781590 597897
5 4 4 9 9
Learn to use the power of Microsoft’s
ground-breaking new technology.
Language Integrated
Query in C# 2008
Pro
Pro LINQ
Language Integrated Query
in C# 2008
■■■
Joseph C. Rattz, Jr.
Rattz_789-3FRONT.fm Page i Thursday, October 25, 2007 8:59 AM
Pro LINQ: Language Integrated Query in C# 2008

Copyright © 2007 by Joseph C. Rattz, Jr.
All rights reserved. No part of this work may be reproduced or transmitted in any form or by any means,
electronic or mechanical, including photocopying, recording, or by any information storage or retrieval
system, without the prior written permission of the copyright owner and the publisher.
ISBN-13 (pbk): 978-1-59059-789-7
ISBN-10 (pbk): 1-59059-789-3
Printed and bound in the United States of America 9 8 7 6 5 4 3 2 1
Trademarked names may appear in this book. Rather than use a trademark symbol with every occurrence
of a trademarked name, we use the names only in an editorial fashion and to the benefit of the trademark
owner, with no intention of infringement of the trademark.
Lead Editor: Ewan Buckingham
Technical Reviewer: Fabio Ferracchiati
Editorial Board: Steve Anglin, Ewan Buckingham, Tony Campbell, Gary Cornell, Jonathan Gennick,
Jason Gilmore, Kevin Goff, Jonathan Hassell, Matthew Moodie, Joseph Ottinger, Jeffrey Pepper,
Ben Renow-Clarke, Dominic Shakeshaft, Matt Wade, Tom Welsh
Senior Project Manager: Tracy Brown Collins
Copy Editors: Heather Lang and Jennifer Whipple
Associate Production Director: Kari Brooks-Copony
Production Editor: Katie Stence
Compositor: Pat Christenson
Proofreader: Dan Shaw
Indexer: Carol Burbo
Artist: Kinetic Publishing Services, LLC
Cover Designer: Kurt Krames
Manufacturing Director: Tom Debolski
Distributed to the book trade worldwide by Springer-Verlag New York, Inc., 233 Spring Street, 6th Floor,
New York, NY 10013. Phone 1-800-SPRINGER, fax 201-348-4505, e-mail , or
visit .
For information on translations, please contact Apress directly at 2855 Telegraph Avenue, Suite 600,
Berkeley, CA 94705. Phone 510-549-5930, fax 510-549-5939, e-mail

, or visit http://
www.apress.com.
The information in this book is distributed on an “as is” basis, without warranty. Although every precaution
has been taken in the preparation of this work, neither the author(s) nor Apress shall have any liability to
any person or entity with respect to any loss or damage caused or alleged to be caused directly or indirectly
by the information contained in this work.
The source code for this book is available to readers at . You will need to answer
questions pertaining to this book in order to successfully download the code.
Rattz_789-3FRONT.fm Page ii Thursday, October 25, 2007 8:59 AM
For my amazing wife Vickey, who managed to keep our house
a home all by herself for these past, long 17 months.
Thank you for doing the things that gave me time to work on this book.
Rattz_789-3FRONT.fm Page iii Thursday, October 25, 2007 8:59 AM
Rattz_789-3FRONT.fm Page iv Thursday, October 25, 2007 8:59 AM
v
Contents at a Glance
About the Author . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xvii
About the Technical Reviewer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xix
Acknowledgments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxi
PART 1 ■ ■ ■ Pro LINQ: Language Integrated
Query in C# 2008
■CHAPTER 1 Hello LINQ . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
■CHAPTER 2 C# 3.0 Language Enhancements for LINQ . . . . . . . . . . . . . . . . . . . . . 19
PART 2 ■ ■ ■ LINQ to Objects
■CHAPTER 3 LINQ to Objects Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 53
■CHAPTER 4 Deferred Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 63
■CHAPTER 5 Nondeferred Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 133
PART 3 ■ ■ ■ LINQ to XML
■CHAPTER 6 LINQ to XML Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 187
■CHAPTER 7 The LINQ to XML API . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 193

■CHAPTER 8 LINQ to XML Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 273
■CHAPTER 9 Additional XML Capabilities . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 301
PART 4 ■ ■ ■ LINQ to DataSet
■CHAPTER 10 LINQ to DataSet Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 339
■CHAPTER 11 Additional DataSet Capabilities . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 373
Rattz_789-3FRONT.fm Page v Thursday, October 25, 2007 8:59 AM
vi
PART 5 ■ ■ ■ LINQ to SQL
■CHAPTER 12 LINQ to SQL Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 381
■CHAPTER 13 LINQ to SQL Tips and Tools . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 391
■CHAPTER 14 LINQ to SQL Database Operations . . . . . . . . . . . . . . . . . . . . . . . . . . . . 415
■CHAPTER 15 LINQ to SQL Entity Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 457
■CHAPTER 16 The DataContext . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 499
■CHAPTER 17 Concurrency Conflicts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 557
■CHAPTER 18 Additional SQL Capabilities . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 573
■INDEX . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 585
Rattz_789-3FRONT.fm Page vi Thursday, October 25, 2007 8:59 AM
vii
Contents
About the Author . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xvii
About the Technical Reviewer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xix
Acknowledgments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxi
PART 1 ■ ■ ■ Pro LINQ: Language Integrated
Query in C# 2008
■CHAPTER 1 Hello LINQ . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
A Paradigm Shift . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
Query XML . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4
Query a SQL Server Database. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5
Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6
LINQ Is About Data Queries . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6

Components . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
How to Obtain LINQ . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8
LINQ Is Not Just for Queries . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8
Tips to Get You Started . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11
Use the var Keyword When Confused . . . . . . . . . . . . . . . . . . . . . . . . 11
Use the Cast or OfType Operators for Legacy Collections . . . . . . . . 12
Prefer the OfType Operator to the Cast Operator . . . . . . . . . . . . . . . 13
Don’t Assume a Query Is Bug-Free . . . . . . . . . . . . . . . . . . . . . . . . . . 14
Take Advantage of Deferred Queries . . . . . . . . . . . . . . . . . . . . . . . . . 15
Use the DataContext Log . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15
Use the LINQ Forum . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16
Rattz_789-3FRONT.fm Page vii Thursday, October 25, 2007 8:59 AM
viii
■CONTENTS
■CHAPTER 2 C# 3.0 Language Enhancements for LINQ . . . . . . . . . . . . . . . . 19
New C# 3.0 Language Additions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19
Lambda Expressions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19
Expression Trees. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24
Keyword var, Object Initialization, and Anonymous Types. . . . . . . . 25
Extension Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 30
Partial Methods. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34
Query Expressions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 37
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 50
PART 2 ■ ■ ■ LINQ to Objects
■CHAPTER 3 LINQ to Objects Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 53
LINQ to Objects Overview . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 53
IEnumerable<T>, Sequences, and the Standard Query Operators . . . . . 54
Returning IEnumerable<T>, Yielding, and Deferred Queries . . . . . . . . . 55
Func Delegates . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 58

The Standard Query Operators Alphabetical Cross-Reference . . . . . . . . 59
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 61
■CHAPTER 4 Deferred Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 63
Referenced Namespaces . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 63
Referenced Assemblies . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 63
Common Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 63
The Deferred Operators by Purpose . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 65
Restriction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 65
Projection. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 67
Partitioning . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 76
Concatenation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 83
Ordering. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 86
Join. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 102
Grouping . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 106
Set . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 111
Conversion. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 117
Element . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 123
Generation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 128
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 131
Rattz_789-3FRONT.fm Page viii Thursday, October 25, 2007 8:59 AM
■CONTENTS
ix
■CHAPTER 5 Nondeferred Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 133
Referenced Namespaces . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 133
Common Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 133
The Nondeferred Operators by Purpose . . . . . . . . . . . . . . . . . . . . . . . . . . 136
Conversion. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 136
Equality . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 148
Element . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 151
Quantifiers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 164

Aggregate . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 170
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 183
PART 3 ■ ■ ■ LINQ to XML
■CHAPTER 6 LINQ to XML Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 187
Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 190
Cheating the W3C DOM XML API . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 190
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 192
■CHAPTER 7 The LINQ to XML API . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 193
Referenced Namespaces . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 193
Significant API Design Enhancements . . . . . . . . . . . . . . . . . . . . . . . . . . . 193
XML Tree Construction Simplified with
Functional Construction
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 194
Document Centricity Eliminated in Favor of
Element Centricity
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 196
Names, Namespaces, and Prefixes . . . . . . . . . . . . . . . . . . . . . . . . . 197
Node Value Extraction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 200
The LINQ to XML Object Model . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 202
Deferred Query Execution, Node Removal, and the
Halloween Problem
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 203
XML Creation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 206
Creating Elements with XElement. . . . . . . . . . . . . . . . . . . . . . . . . . . 206
Creating Attributes with XAttribute . . . . . . . . . . . . . . . . . . . . . . . . . . 208
Creating Comments with XComment . . . . . . . . . . . . . . . . . . . . . . . . 209
Creating Containers with XContainer . . . . . . . . . . . . . . . . . . . . . . . . 210
Creating Declarations with XDeclaration . . . . . . . . . . . . . . . . . . . . . 210
Creating Document Types with XDocumentType . . . . . . . . . . . . . . 211
Rattz_789-3FRONT.fm Page ix Thursday, October 25, 2007 8:59 AM

x
■CONTENTS
Creating Documents with XDocument . . . . . . . . . . . . . . . . . . . . . . . 212
Creating Names with XName . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 213
Creating Namespaces with XNamespace . . . . . . . . . . . . . . . . . . . . 214
Creating Nodes with XNode. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 214
Creating Processing Instructions with XProcessingInstruction . . . 214
Creating Streaming Elements with XStreamingElement . . . . . . . . 216
Creating Text with XText . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 217
Creating CData with XCData . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 218
XML Output . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 218
Saving with XDocument.Save() . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 218
Saving with XElement.Save(). . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 219
XML Input . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 220
Loading with XDocument.Load(). . . . . . . . . . . . . . . . . . . . . . . . . . . . 220
Loading with XElement.Load(). . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 222
Parsing with XDocument.Parse() or XElement.Parse() . . . . . . . . . . 223
XML Traversal . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 224
Traversal Properties . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 225
Traversal Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 228
XML Modification . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 242
Adding Nodes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 242
Deleting Nodes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 247
Updating Nodes. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 249
XElement.SetElementValue() on Child XElement Objects. . . . . . . . 253
XML Attributes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 255
Attribute Creation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 255
Attribute Traversal . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 255
Attribute Modification. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 258
XML Annotations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 263

Adding Annotations with XObject.AddAnnotation() . . . . . . . . . . . . . 263
Accessing Annotations with XObject.Annotation() or
XObject.Annotations()
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 263
Removing Annotations with XObject.RemoveAnnotations() . . . . . . 264
Annotations Example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 264
XML Events . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 267
XObject.Changing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 268
XObject.Changed . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 268
A Couple of Event Examples . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 268
Trick or Treat, or Undefined?. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 272
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 272
Rattz_789-3FRONT.fm Page x Thursday, October 25, 2007 8:59 AM
■CONTENTS
xi
■CHAPTER 8 LINQ to XML Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 273
Introduction to LINQ to XML Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . 273
Ancestors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 274
Prototypes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 274
Examples . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 274
AncestorsAndSelf . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 278
Prototypes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 278
Examples . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 278
Attributes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 280
Prototypes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 280
Examples . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 281
DescendantNodes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 283
Prototypes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 283
Examples . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 283
DescendantNodesAndSelf . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 284

Prototypes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 284
Examples . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 284
Descendants . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 286
Prototypes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 286
Examples . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 286
DescendantsAndSelf . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 288
Prototypes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 288
Examples . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 288
Elements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 290
Prototypes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 291
Examples . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 291
InDocumentOrder . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 293
Prototypes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 293
Examples . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 293
Nodes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 294
Prototypes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 295
Examples . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 295
Remove . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 296
Prototypes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 296
Examples . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 296
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 299
Rattz_789-3FRONT.fm Page xi Thursday, October 25, 2007 8:59 AM
xii
■CONTENTS
■CHAPTER 9 Additional XML Capabilities . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 301
Referenced Namespaces . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 301
Queries . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 301
No Reaching . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 302
A Complex Query . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 304
Transformations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 309

Transformations Using XSLT . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 310
Transformations Using Functional Construction . . . . . . . . . . . . . . . 312
Tips. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 314
Validation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 320
The Extension Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 320
Prototypes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 320
Obtaining an XML Schema . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 321
Examples . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 322
XPath . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 334
Prototypes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 334
Examples . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 335
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 335
PART 4 ■ ■ ■ LINQ to DataSet
■CHAPTER 10 LINQ to DataSet Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 339
Assembly References . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 340
Referenced Namespaces . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 340
Common Code for the Examples . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 340
DataRow Set Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 341
Distinct. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 342
Except . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 345
Intersect. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 347
Union . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 349
SequenceEqual . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 352
DataRow Field Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 353
Field<T> . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 357
SetField<T> . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 362
DataTable Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 365
AsEnumerable. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 365
CopyToDataTable<DataRow>. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 366
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 371

Rattz_789-3FRONT.fm Page xii Thursday, October 25, 2007 8:59 AM
■CONTENTS
xiii
■CHAPTER 11 Additional DataSet Capabilities . . . . . . . . . . . . . . . . . . . . . . . . . 373
Required Namespaces . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 373
Typed DataSets . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 373
Putting It All Together . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 375
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 378
PART 5 ■ ■ ■ LINQ to SQL
■CHAPTER 12 LINQ to SQL Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 381
Introducing LINQ to SQL . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 382
The DataContext . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 383
Entity Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 384
Associations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 384
Concurrency Conflict Detection. . . . . . . . . . . . . . . . . . . . . . . . . . . . . 385
Concurrency Conflict Resolution. . . . . . . . . . . . . . . . . . . . . . . . . . . . 385
Prerequisites for Running the Examples . . . . . . . . . . . . . . . . . . . . . . . . . 386
Obtaining the Appropriate Version of the
Northwind Database
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 386
Generating the Northwind Entity Classes. . . . . . . . . . . . . . . . . . . . . 386
Generating the Northwind XML Mapping File . . . . . . . . . . . . . . . . . 387
Using the LINQ to SQL API . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 388
IQueryable<T> . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 388
Some Common Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 388
GetStringFromDb() . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 388
ExecuteStatementInDb(). . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 389
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 390
■CHAPTER 13 LINQ to SQL Tips and Tools . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 391
Introduction to LINQ to SQL Tips and Tools . . . . . . . . . . . . . . . . . . . . . . . 391

Tips . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 391
Use the DataContext.Log Property . . . . . . . . . . . . . . . . . . . . . . . . . . 391
Use the GetChangeSet() Method . . . . . . . . . . . . . . . . . . . . . . . . . . . . 393
Consider Using Partial Classes or Mapping Files . . . . . . . . . . . . . . 393
Consider Using Partial Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . 393
Rattz_789-3FRONT.fm Page xiii Thursday, October 25, 2007 8:59 AM
xiv
■CONTENTS
Tools . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 393
SQLMetal . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 393
The Object Relational Designer. . . . . . . . . . . . . . . . . . . . . . . . . . . . . 400
Use SQLMetal and the O/R Designer Together . . . . . . . . . . . . . . . . . . . . 413
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 413
■CHAPTER 14 LINQ to SQL Database Operations . . . . . . . . . . . . . . . . . . . . . . . 415
Prerequisites for Running the Examples . . . . . . . . . . . . . . . . . . . . . . . . . 415
Some Common Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 415
Using the LINQ to SQL API . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 415
Standard Database Operations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 416
Inserts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 416
Queries. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 420
Updates . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 443
Deletes. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 446
Overriding Database Modification Statements . . . . . . . . . . . . . . . . . . . . 449
Overriding the Insert Method. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 449
Overriding the Update Method . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 450
Overriding the Delete Method . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 450
Example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 450
Overriding in the Object Relational Designer. . . . . . . . . . . . . . . . . . 453
Considerations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 453
SQL Translation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 453

Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 455
■CHAPTER 15 LINQ to SQL Entity Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 457
Prerequisites for Running the Examples . . . . . . . . . . . . . . . . . . . . . . . . . 457
Entity Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 457
Creating Entity Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 457
XML External Mapping File Schema. . . . . . . . . . . . . . . . . . . . . . . . . 484
Projecting into Entity Classes vs. Nonentity Classes . . . . . . . . . . . 485
Extending Entity Classes with Partial Methods . . . . . . . . . . . . . . . . . . . . 490
Important System.Data.Linq API Classes . . . . . . . . . . . . . . . . . . . . . . . . . 492
EntitySet<T>. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 492
EntityRef<T>. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 492
Table<T>. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 494
IExecuteResult. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 495
ISingleResult<T> . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 496
IMultipleResults. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 496
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 497
Rattz_789-3FRONT.fm Page xiv Thursday, October 25, 2007 8:59 AM
■CONTENTS
xv
■CHAPTER 16 The DataContext . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 499
Prerequisites for Running the Examples . . . . . . . . . . . . . . . . . . . . . . . . . 499
Some Common Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 499
Using the LINQ to SQL API . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 499
[Your]DataContext Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 499
The DataContext Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 500
Primary Purposes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 503
DataContext() and [Your]DataContext(). . . . . . . . . . . . . . . . . . . . . . . 509
SubmitChanges(). . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 521
DatabaseExists() . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 527
CreateDatabase() . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 528

DeleteDatabase(). . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 530
CreateMethodCallQuery(). . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 530
ExecuteQuery() . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 532
Translate() . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 534
ExecuteCommand() . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 536
ExecuteMethodCall(). . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 537
GetCommand() . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 544
GetChangeSet() . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 545
GetTable() . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 547
Refresh() . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 549
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 555
■CHAPTER 17 Concurrency Conflicts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 557
Prerequisites for Running the Examples . . . . . . . . . . . . . . . . . . . . . . . . . 557
Some Common Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 557
Using the LINQ to SQL API . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 557
Concurrency Conflicts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 557
Optimistic Concurrency . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 558
Pessimistic Concurrency . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 568
An Alternative Approach for Middle Tiers and Servers. . . . . . . . . . 570
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 572
Rattz_789-3FRONT.fm Page xv Thursday, October 25, 2007 8:59 AM
xvi
■CONTENTS
■CHAPTER 18 Additional SQL Capabilities . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 573
Prerequisites for Running the Examples . . . . . . . . . . . . . . . . . . . . . . . . . 573
Using the LINQ to SQL API . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 573
Using the LINQ to XML API. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 573
Database Views . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 573
Entity Class Inheritance . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 575
Transactions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 580

Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 582
■INDEX . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 585
Rattz_789-3FRONT.fm Page xvi Thursday, October 25, 2007 8:59 AM
xvii
About the Author
■JOSEPH C. RATTZ, JR., unknowingly began his career in software development in 1990 when a friend
asked him for assistance writing an ANSI text editor named ANSI Master for the Commodore Amiga.
A hangman game (The Gallows) soon followed. From these compiled Basic programs, he moved on
to programming in C for more speed and power. Joe then developed applications that were sold to
JumpDisk, an Amiga disk magazine, as well as Amiga World magazine. Due to developing in a small
town on a fairly isolated platform, Joe learned all the wrong ways to write code. It was while trying to
upgrade his poorly written applications that he gained respect for the importance of easily maintain-
able code. It was love at first sight when Joe spotted a source-level debugger in use for the first time.
Two years later, Joe obtained his first software development opportunity at Policy Management
Systems Corporation as an entry-level programmer developing a client/server insurance application
for OS/2 and Presentation Manager. Through the years, he added C++, Unix, Java, ASP, ASP.NET, C#,
HTML, DHTML, and XML to his skill set while developing applications for SCT, DocuCorp, IBM and
the Atlanta Committee for the Olympic Games, CheckFree, NCR, EDS, Delta Technology, Radiant
Systems, and the Genuine Parts Company. Joe enjoys the creative aspects of user interface design,
and he appreciates the discipline necessary for server-side development. But, given his druthers, his
favorite development pastime is debugging code.
Joe can be found working for the Genuine Parts Company—the parent company of NAPA—
in the Automotive Parts Group Information Systems department, where he works on his baby, the
Storefront web site. This site for NAPA stores provides a view into their accounts and data on a network
of AS/400s.
Joe can be reached at his web site, www.linqdev.com.
Rattz_789-3FRONT.fm Page xvii Thursday, October 25, 2007 8:59 AM
Rattz_789-3FRONT.fm Page xviii Thursday, October 25, 2007 8:59 AM
xix
About the Technical Reviewer

■FABIO CLAUDIO FERRACCHIATI is a senior consultant and a senior analyst/developer using Microsoft
technologies. He works for Brain Force (www.brainforce.com
) in its Italian branch (www.brainforce.it).
He is a Microsoft Certified Solution Developer for .NET, a Microsoft Certified Application Developer
for .NET, a Microsoft Certified Professional, and a prolific author and technical reviewer. Over the
past ten years, he’s written articles for Italian and international magazines and coauthored more
than ten books on a variety of computer topics. You can read his LINQ blog at
www.ferracchiati.com.

Rattz_789-3FRONT.fm Page xix Thursday, October 25, 2007 8:59 AM
Rattz_789-3FRONT.fm Page xx Thursday, October 25, 2007 8:59 AM
xxi
Acknowledgments
I had wanted to write a book for years but could never find a topic I felt was appropriate for me to
write about. Either I knew the material well because it was old hat and didn’t need yet another book,
or I knew nothing about the topic and felt unqualified to write about it.
When I first learned of LINQ in September of 2005 by watching that first LINQ video with Anders
Hejlsberg, I immediately saw the revolutionary effect this would have on .NET development. Little
did I know that a mere seven months later I would be writing a book about LINQ. Initially, I had no
intention of writing a book about LINQ, but I did want to write a book. One day I had one of those
programming-related email newsletters sitting in my inbox, and it was about the May 2006 LINQ
Community Technology Preview. I realized that no one could know that much more about LINQ
than me because it was so new. Other than Microsoft employees, no one could have had more than
a few months experience with LINQ. It seemed the appropriate time to write a book, so despite
knowing nothing about LINQ other than what I saw in that video, I signed a contract with Apress to
write a book about LINQ. What could I have been thinking?
This book, which started out as a nine-month project, grew into a 17-month project. You can’t
dedicate 17 months of your life as an adult without a lot of support. For me, I couldn’t have done it
without my wife Vickey. For 17 months she took care of most of my chores and pulled my weight
around the house. For 17 months, she heard me say, “I can’t; I have to work on my book.” That’s a

long time. I couldn’t have done it without her, nor would I have wanted to.
I want to say thank you to all the fabulous people at Apress that had to endure my writing and
personality. No one paid the price for this more than Tracy Brown Collins. She did her best to keep
me focused and on schedule. Through four releases of LINQ/Orcas she managed to keep track of
everything and tolerate my alarmist reactions.
Next on the list at Apress is my technical reviewer Fabio Claudio Ferracchiati. If the name is
familiar to you, it may be because Fabio has written two LINQ books himself. At first, I was not keen
on the idea of my technical reviewer being an author for another LINQ book. The thought of handing
another LINQ author all my work just seemed like a bad idea. I do believe this book is better though
because of Fabio’s LINQ knowledge.
This being my first book, I certainly needed first-class guidance and probably could not have
found better than Ewan Buckingham. Ewan’s experience helped ease stressful situations that I did
not know how to handle. Ewan was also willing to indulge some of my less-than-standard requests,
such as starting the book with code. I like that this book starts with code, and I hope you will as well.
I also cannot forget to thank my copy editors, Heather Lang and Jennifer Whipple. Although
frustrating at times, having someone polish everything you write is a luxury I wish I had available
every time I write. If there is one thing I have learned from working with them, it’s that I like commas
way, too, much.
I want to thank Katie Stence and her team. Chapter 2 proved to be quite difficult and required a
lot of effort from her team. You can thank them for the fact that the code translations are presented
as nicely as they are.
I also want to thank Apress for the opportunity to write a book. I don’t get the opportunity to do
that every day. It was painful but very rewarding.
Of course, this book wouldn’t be possible were it not for Microsoft and all the talented and dedi-
cated developers there who take the time to respond to posts on the MSDN LINQ forum. First, I want
to thank Matt Warren, who seems to devour the lion’s share of the posts. He also saw to it that Visual
Rattz_789-3FRONT.fm Page xxi Thursday, October 25, 2007 8:59 AM
xxii
■ACKNOWLEDGMENTS
Studio 2008 (and LINQ) was launched on my birthday per my request, so how could I not thank him

for that?
Also, I want to thank Keith Farmer, Dinesh Kulkarni, Mads Torgersen, and Eric White, who all
provided answers, information, and the occasional guidance. I am sure there are countless,
unnamed others at Microsoft who contributed significantly to LINQ. To them, I say thank you. Last
at Microsoft, I want to thank Anders Hejlsberg for the vision from which LINQ grew.
This book also would not have been possible without the inspiration and trail blazing of my
friend and author Bruce Bukovics. Be sure to check out his titles. Having worked with Bruce, I can
assure you he has a lot of information and wisdom to share.
This book more than likely would not have happened were it not for my parents, because my
dad convinced me I can do anything I set my mind to, and because my mom made sure I did well in
school, even if it meant her teaching me if I fell behind. I can tell you that nothing is more important
for a child’s education than involved and caring parents.
Having mentioned the importance of parents in my education, it would be an injustice to not
mention some of the exceptional teachers that made a difference in my life. First and foremost would
have to be Susan Hadley, my sixth grade teacher. Mrs. Hadley taught me that learning can be fun.
That was an important lesson. I have made every effort for this book to be fun, which is probably the
direct result of her being my teacher. If you enjoy the occasional reference or joke in this book, you
can thank her. Next on my list is Ruby Johnson. When other teachers were content to let me coast at
the pace of the other students, she went to the extra effort of teaching me individually, which meant
individual lessons, homework assignments, and tests. That probably doubled her workload for that
class. Next is Nancy Cooper. Again, Mrs. Cooper taught me that education does not have to be dull.
It can be interesting, exciting, and doesn’t even have to be confined to a textbook.
This book would have never been possible were it not for the many excellent colleagues I have
worked with and learned from. Most important were Mike Furnell, James Richardson, John Proctor,
and Brad Radaker. Mike, my first technical lead, asked me what I was weakest at in the C language.
When I told him pointers, he told me to debug our link-list library on paper. If you read about 2000
lines of link-list library code, you will either learn pointers or go mad, and those may not be mutually
exclusive. James Richardson, with six months of experience, had one of the single greatest knacks at
programming I have ever seen. He seemed to absorb programming, languages, and concepts. He
taught me everything I understand about abstraction and code reuse. John Proctor may be the sharpest

programming mind I have ever met. There were so many times he would call me in the morning to
ask me about some technology I typically knew nothing about. By the time I would call him later that
day, he would have downloaded some open source implementation, built it, tested it, tweaked the
code, dumped it, and written his own. Brad Radaker has been a constant source of information and
guidance. Even topics I wouldn’t expect him to remember he can recall years later. On any given day,
he can probably explain how my code works better than I can. All of these guys have been a tremen-
dous source of knowledge and inspiration.
Last I want to thank RJ Mical, who once dared me to be gorgeous and unique. From his challenge,
I learned that I can be.
Rattz_789-3FRONT.fm Page xxii Thursday, October 25, 2007 8:59 AM
■ ■ ■
PART 1
Pro LINQ: Language
Integrated Query
in C# 2008
Rattz_789-3C01.fm Page 1 Tuesday, October 2, 2007 2:29 PM

×