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
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
netbooks.wordpress.com
Codered @ Updatesofts.com
class="bi x0 y48 w1 h1a"
Pro LINQ
Language Integrated Query
in C# 2008
■■■
Joseph C. Rattz, Jr.
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
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
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
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
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
■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
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
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
■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