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

640 pro LINQ

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.46 MB, 746 trang )

CYAN
MAGENTA

YELLOW
BLACK
PANTONE 123 C

BOOKS FOR PROFESSIONALS BY PROFESSIONALS ®

Joseph C. Rattz, Jr.,
author of
Pro LINQ: Language
Integrated Query
in C# 2008

Pro LINQ:
Language Integrated Query in VB 2008

Joseph C. Rattz, Jr.

THE APRESS ROADMAP
Pro VB 2008 and
the .NET 3.5 Platform
Beginning VB 2008
Pro LINQ in VB 2008

Language Integrated
Query in VB 2008

Pro LINQ: Language Integrated Query in VB 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 declarations 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 demonstrating 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 VB.NET who
wants to understand LINQ and LINQ-relevant VB.NET 9.0 language features.
You need not be up on all the latest VB.NET 8.0 or 9.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.

Pro

LINQ

Dear Reader,

THE EXPERT’S VOICE ® IN .NET

Pro

LINQ
Language Integrated Query in VB 2008
Learn to use the power of Microsoft’s

ground-breaking new technology.

Expert VB 2008
Business Objects

Beginning
VB 2008 Databases
Pro WPF in VB 2008

SOURCE CODE ONLINE

www.apress.com

ISBN 978-1-4302-1644-5
54499

US $44.99

Rattz
Hayes

Joseph C. Rattz, Jr.
and Dennis Hayes

Shelve in
Programming/VB 2008
User level:
Intermediate–Advanced

9 781430 216445


www.it-ebooks.info

this print for content only—size & color not accurate

spine = 1.40625" 744 page count


www.it-ebooks.info


Pro LINQ
Language Integrated Query
in VB 2008

Joseph C. Rattz, Jr. and Dennis Hayes

www.it-ebooks.info
16445fm_CMP2.indd 1

7/17/09 10:51:53 AM


Pro LINQ: Language Integrated Query in VB 2008
Copyright © 2009 by Joseph C. Rattz, Jr. and Dennis Hayes
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-4302-1644-5
ISBN-13 (electronic): 978-1-4302-1645-2

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 Editor: Joseph C. Rattz, Jr.
Technical Reviewers: Joseph C. Rattz, Jr., Fabio Ferracchiati
Editorial Board: Clay Andres, Steve Anglin, Mark Beckner, Ewan Buckingham, Tony Campbell,
Gary Cornell, Jonathan Gennick, Michelle Lowman, Matthew Moodie, Jeffrey Pepper,
Frank Pohlmann, Ben Renow-Clarke, Dominic Shakeshaft, Matt Wade, Tom Welsh
Project Manager: Richard Dal Porto
Copy Editor: Heather Lang
Associate Production Director: Kari Brooks-Copony
Production Editor: Kelly Winquist
Compositors: Dina Quan, Patrick Cunningham
Proofreader: April Eddy
Indexer: Carol Burbo
Artist: April Milne
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 kn`ano)juvisit dppl6++sss*olnejcankjheja*_ki.
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 ejbk<]lnaoo*_ki, or visit dppl6++sss*
]lnaoo*_ki.
Apress and friends of ED books may be purchased in bulk for academic, corporate, or promotional use.
eBook versions and licenses are also available for most titles. For more information, reference our Special
Bulk Sales–eBook Licensing web page at dppl6++sss*]lnaoo*_ki+ejbk+^qhgo]hao.
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 dppl6++sss*]lnaoo*_ki. You may need to answer
questions pertaining to this book in order to successfully download the code.

www.it-ebooks.info
16445fm_CMP2.indd 2

7/17/09 10:51:53 AM


To my father-in-law, Samuel Arthur Sanders, I dedicate this book. Art Sanders was my
most supportive cheerleader while I was working on Pro LINQ: Language Integrated Query
in C# 2008. Unfortunately, Art’s health began to fail toward the end of that book, and for
a while, it seemed doubtful he would live to see it completed. Not one to give up, Art overcame numerous hospitalizations and did indeed receive his personally signed copy.
Sadly, Art will never see this book because he passed away on February 25, 2009, at the
age of 80. Rest in peace, Art.

www.it-ebooks.info
16445fm_CMP2.indd 3

7/17/09 10:51:53 AM


Contents at a Glance
About the Author . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxiii
About the Technical Reviewer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxv
Acknowledgments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .xxvii

PART 1 NNN Pro LINQ: Language Integrated Query
in VB.NET 2008

CHAPTER 1

Hello LINQ . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3

CHAPTER 2

VB.NET 2008 Language Enhancements for LINQ . . . . . . . . . . . . . . . . . 21

PART 2 NNN LINQ to Objects
CHAPTER 3

LINQ to Objects Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 81

CHAPTER 4

Deferred Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 93

CHAPTER 5

Nondeferred Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 177

PART 3 NNN LINQ to XML
CHAPTER 6

LINQ to XML Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 241

CHAPTER 7

The LINQ to XML API . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 247


CHAPTER 8

LINQ to XML Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 337

CHAPTER 9

Additional XML Capabilities . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 369

iv

www.it-ebooks.info
16445fm_CMP2.indd 4

7/17/09 10:51:53 AM


Part 4 NNN LINQ to DataSet
CHAPTER 10

LINQ to DataSet Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 413

CHAPTER 11

Additional DataSet Capabilities . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 453

Part 5 NNN LINQ to SQL
CHAPTER 12

LINQ to SQL Introduction. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 463


CHAPTER 13

LINQ to SQL Tips and Tools . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 477

CHAPTER 14

LINQ to SQL Database Operations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 505

CHAPTER 15

LINQ to SQL Entity Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 553

CHAPTER 16

The DataContext . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 599

CHAPTER 17

Concurrency Conflicts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 663

CHAPTER 18

Additional SQL Capabilities . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 681

INDEX . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 695

v

www.it-ebooks.info
16445fm_CMP2.indd 5


7/17/09 10:51:53 AM


www.it-ebooks.info
16445fm_CMP2.indd 6

7/17/09 10:51:53 AM


Contents
About the Author . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxiii
About the Technical Reviewer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxv
Acknowledgments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .xxvii

PART 1
CHAPTER 1

NNN

Pro LINQ: Language Integrated
Query in VB.NET 2008

Hello LINQ. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
A Paradigm Shift . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4
Query XML . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4
Query a SQL Server Database . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6
Introduction to LINQ. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
LINQ Is About Data Queries . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8
Components . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8

LINQ to Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8
LINQ to XML. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9
LINQ to DataSet . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9
LINQ to SQL . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9
LINQ to Entities . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9
How to Obtain LINQ . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9
LINQ Is Not Just for Queries . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10
Tips to Get You Started . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13
Use Dim Without Specifying the Type When Confused . . . . . . . . . . . 13
Use the Cast or OfType Operators for Legacy Collections. . . . . . . . . 15
Prefer the OfType Operator to the Cast Operator . . . . . . . . . . . . . . . . 16
Don’t Assume a Query Is Bug Free . . . . . . . . . . . . . . . . . . . . . . . . . . . 16
Take Advantage of Deferred Queries . . . . . . . . . . . . . . . . . . . . . . . . . . 17
Use the DataContext Log . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18
Use the LINQ Forum. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19
vii

www.it-ebooks.info
16445fm_CMP2.indd 7

7/17/09 10:51:53 AM


viii

NCO NTENT S

CHAPTER 2


VB.NET 2008 Language Enhancements for LINQ . . . . . . . . . . 21
New VB.NET 2008 Language Additions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21
New (and Not So New) Options . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22
Option Explicit . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22
Option Compare . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23
Option Strict. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23
Option Infer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24
Lambda Expressions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24
Using Named Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25
Using Lambda Expressions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26
Expression Trees . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 29
Keyword Dim, Object Initialization, and Anonymous Types . . . . . . . 30
The Implicitly Typed Local Variable Keyword Dim
(with Option Infer On/Option Strict On) . . . . . . . . . . . . . . . . . 31
Object Initialization Expressions. . . . . . . . . . . . . . . . . . . . . . . . . . 33
Anonymous Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34
Extension Methods. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 38
Instance (Object) vs. Shared (Class) Methods Recap . . . . . . . . 38
The Problem Solved by Extension Methods . . . . . . . . . . . . . . . . 40
The Solution . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 41
Extension Method Declarations and Invocations. . . . . . . . . . . . 42
Extension Method Precedence . . . . . . . . . . . . . . . . . . . . . . . . . . . 43
Partial Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 43
A Partial Method Example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 44
What Is the Point of Partial Methods? . . . . . . . . . . . . . . . . . . . . . 46
The Rules . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 47
Query Expressions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 47
Query Expression Grammar . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 49
Query Expression Translation . . . . . . . . . . . . . . . . . . . . . . . . . . . . 51
Nullable Value Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 60

XML Enhancements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 61
XML Literals. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 61
XML Embedded Expressions . . . . . . . . . . . . . . . . . . . . . . . . . . . . 64
XML Axis Properties . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 67
XML Namespace Imports . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 74
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 77

www.it-ebooks.info
16445fm_CMP2.indd 8

7/17/09 10:51:53 AM


NC O N T E N T S

PART 2
CHAPTER 3

NNN

ix

LINQ to Objects

LINQ to Objects Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 81
LINQ to Objects Overview . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 81
IEnumerable(Of T), Sequences, and the Standard Query Operators . . . . . 82
Returning IEnumerable(Of T) and Deferred Queries . . . . . . . . . . . . . . . . . . 83
Func Delegates . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 87
The Standard Query Operators Alphabetical Cross-Reference . . . . . . . . . 88

A Tale of Two Syntaxes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 90
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 90

CHAPTER 4

Deferred Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 93
Referenced Namespaces . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 93
Referenced Assemblies . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 93
Query Expression Syntax vs. Standard Dot Notation Syntax . . . . . . . . . . . 94
Common Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 94
The Deferred Operators by Purpose . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 96
Restriction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 96
Where . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 96
Projection. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 99
Select . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 99
SelectMany . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 105
Partitioning . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 110
Take . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 110
TakeWhile . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 113
Skip . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 115
SkipWhile . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 116
Concatenation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 119
Concat . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 119
Ordering . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 122
OrderBy . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 123
OrderByDescending . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 129
ThenBy . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 132
ThenByDescending . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 137
Reverse . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 140


www.it-ebooks.info
16445fm_CMP2.indd 9

7/17/09 10:51:53 AM


x

NCO NTENT S

Join . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 142
Join . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 142
GroupJoin. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 144
Grouping . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 146
GroupBy . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 146
Set . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 152
Distinct . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 153
Union . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 154
Intersect . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 156
Except . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 157
Conversion. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 159
Cast . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 159
OfType . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 161
AsEnumerable . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 164
Element . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 167
DefaultIfEmpty. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 167
Generation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 172
Range . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 172
Repeat . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 173
Empty . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 174

Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 175

CHAPTER 5

Nondeferred Operators

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 177

Referenced Namespaces . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 177
Common Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 177
The Nondeferred Operators by Purpose. . . . . . . . . . . . . . . . . . . . . . . . . . . . 180
Conversion. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 181
ToArray . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 181
ToList . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 183
ToDictionary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 185
ToLookup . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 190
Equality . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 195
SequenceEqual . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 195
Element . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 199
First . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 199
FirstOrDefault . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 201
Last . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 203
LastOrDefault . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 205
Single . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 207

www.it-ebooks.info
16445fm_CMP2.indd 10

7/17/09 10:51:53 AM



NC O N T E N T S

xi

SingleOrDefault . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 209
ElementAt . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 211
ElementAtOrDefault . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 212
Quantifiers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 213
Any . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 214
All . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 216
Contains . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 217
Aggregate . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 220
Count . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 220
LongCount . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 222
Sum . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 224
Min . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 226
Max . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 229
Average . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 232
Aggregate . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 234
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 238

PART 3
CHAPTER 6

NNN

LINQ to XML

LINQ to XML Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 241

Additional Advantages of LINQ to XML. . . . . . . . . . . . . . . . . . . . . . . . . . . . . 244
Cheating the W3C DOM XML API . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 244
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 246

CHAPTER 7

The LINQ to XML API . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 247
Referenced Namespaces . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 247
Significant API Design Enhancements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 248
XML Tree Construction Simplified with
Functional Construction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 248
Document Centricity Eliminated in Favor of
Element Centricity. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 251
Names, Namespaces, and Prefixes . . . . . . . . . . . . . . . . . . . . . . . . . . 253
Node Value Extraction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 255
The LINQ to XML Object Model . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 258
Deferred Query Execution, Node Removal, and
the Halloween Problem . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 259

www.it-ebooks.info
16445fm_CMP2.indd 11

7/17/09 10:51:53 AM


xii

NCO NTENT S

XML Creation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 262

Creating XML with XML Literals . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 263
Creating Elements with XElement . . . . . . . . . . . . . . . . . . . . . . . . . . . . 263
Creating Attributes with XAttribute . . . . . . . . . . . . . . . . . . . . . . . . . . . 266
Creating Comments with XComment . . . . . . . . . . . . . . . . . . . . . . . . . 267
Creating Containers with XContainer . . . . . . . . . . . . . . . . . . . . . . . . . 268
Creating Declarations with XDeclaration . . . . . . . . . . . . . . . . . . . . . . 268
Creating Document Types with XDocumentType . . . . . . . . . . . . . . . 269
Creating Documents with XDocument . . . . . . . . . . . . . . . . . . . . . . . . 270
Creating Names with XName . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 271
Creating Namespaces with XNamespace . . . . . . . . . . . . . . . . . . . . . 272
Creating Nodes with XNode . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 272
Creating Processing Instructions with XProcessingInstruction . . . 273
Creating Streaming Elements with XStreamingElement . . . . . . . . . 275
Creating Text with XText . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 276
Creating CData with XCData . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 277
XML Output . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 277
Saving with XDocument.Save() . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 277
Saving with XElement.Save() . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 279
XML Input . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 280
Loading with XDocument.Load() . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 280
Loading with XElement.Load() . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 282
Parsing with XDocument.Parse() or XElement.Parse() . . . . . . . . . . . 282
XML Traversal . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 283
Traversal Properties. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 284
Forward with XNode.NextNode . . . . . . . . . . . . . . . . . . . . . . . . . 284
Backward with XNode.PreviousNode . . . . . . . . . . . . . . . . . . . . 285
Up to Document with XObject.Document . . . . . . . . . . . . . . . . . 286
Up with XObject.Parent . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 287
Traversal Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 288
Down with XContainer.Nodes() . . . . . . . . . . . . . . . . . . . . . . . . . 288

Down with XContainer.Elements() . . . . . . . . . . . . . . . . . . . . . . . 292
Down with XContainer.Element() . . . . . . . . . . . . . . . . . . . . . . . . 294
Up Recursively with XNode.Ancestors() . . . . . . . . . . . . . . . . . . 295
Up Recursively with XElement.AncestorsAndSelf() . . . . . . . . . 296
Down Recursively with XContainer.Descendants() . . . . . . . . . 297
Down Recursively with XElement.DescendantsAndSelf() . . . 298
Forward with XNode.NodesAfterSelf(). . . . . . . . . . . . . . . . . . . . 298
Forward with XNode.ElementsAfterSelf() . . . . . . . . . . . . . . . . . 300
Backward with XNode.NodesBeforeSelf() . . . . . . . . . . . . . . . . 300
Backward with XNode.ElementsBeforeSelf() . . . . . . . . . . . . . . 302
www.it-ebooks.info
16445fm_CMP2.indd 12

7/17/09 10:51:54 AM


NC O N T E N T S

xiii

XML Modification . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 302
Adding Nodes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 303
XContainer.Add() (AddLast) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 303
XContainer.AddFirst() . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 304
XNode.AddBeforeSelf() . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 305
XNode.AddAfterSelf() . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 307
Deleting Nodes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 308
XNode.Remove() . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 308
IEnumerable(Of T).Remove() . . . . . . . . . . . . . . . . . . . . . . . . . . . . 309
XElement.RemoveAll() . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 310

Updating Nodes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 311
XElement.Value on XElement Objects, XText.Value
on XText Objects, and XComment.Value on
XComment Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 311
XDocumentType.Name, XDocumentType.PublicId,
XDocumentType.SystemId, and XDocumentType.
InternalSubset on XDocumentType Objects . . . . . . . . . . . . 313
XProcessingInstruction.Target on XProcessingInstruction
Objects and XProcessingInstruction.Data on
XProcessingInstruction Objects . . . . . . . . . . . . . . . . . . . . . . 313
XElement.ReplaceAll() . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 314
XElement.SetElementValue() on Child XElement Objects . . . . . . . . 315
XML Attributes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 317
Attribute Creation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 317
Attribute Traversal . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 317
Forward with XElement.FirstAttribute . . . . . . . . . . . . . . . . . . . . 317
Forward with XAttribute.NextAttribute . . . . . . . . . . . . . . . . . . . 318
Backward with XAttribute.PreviousAttribute . . . . . . . . . . . . . . 318
Backward with XElement.LastAttribute . . . . . . . . . . . . . . . . . . 319
XElement.Attribute() . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 319
XElement.Attributes() . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 320
Attribute Modification . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 321
Adding Attributes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 321
Deleting Attributes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 321
Updating Attributes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 324
XElement.SetAttributeValue() . . . . . . . . . . . . . . . . . . . . . . . . . . . 325
XML Annotations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 327
Adding Annotations with XObject.AddAnnotation() . . . . . . . . . . . . . . 327
Accessing Annotations with XObject.Annotation() or
XObject.Annotations() . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 327


www.it-ebooks.info
16445fm_CMP2.indd 13

7/17/09 10:51:54 AM


xiv

NCO NTENT S

Removing Annotations with XObject.RemoveAnnotations() . . . . . . 327
Annotations Example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 328
XML Events . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 331
XObject.Changing. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 331
XObject.Changed . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 332
An Event Example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 332
Trick or Treat, or Undefined? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 335
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 335

CHAPTER 8

LINQ to XML Operators. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 337
Introduction to LINQ to XML Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . 337
Ancestors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 338
Declarations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 338
Examples . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 339
AncestorsAndSelf. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 343
Declarations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 343
Examples . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 344

Attributes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 346
Declarations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 346
Examples . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 347
DescendantNodes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 348
Declarations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 349
Examples . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 349
DescendantNodesAndSelf . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 350
Declarations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 350
Examples . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 351
Descendants . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 352
Declarations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 352
Examples . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 352
DescendantsAndSelf . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 354
Declarations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 355
Examples . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 355
Elements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 357
Declarations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 358
Examples . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 358
InDocumentOrder. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 360
Declarations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 360
Examples . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 360

www.it-ebooks.info
16445fm_CMP2.indd 14

7/17/09 10:51:54 AM


NC O N T E N T S


xv

Nodes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 362
Declarations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 362
Examples . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 362
Remove . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 363
Declarations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 363
Examples . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 364
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 366

Additional XML Capabilities . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 369

CHAPTER 9

Referenced Namespaces . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 369
Queries . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 370
No Reaching . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 370
A Complex Query . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 373
Transformations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 379
Transformations Using XSLT . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 379
Transformations Using Functional Construction . . . . . . . . . . . . . . . . 381
Tips On XML Transformations Using LINQ . . . . . . . . . . . . . . . . . . . . . 384
Simplify Complex Tasks with Helper Methods . . . . . . . . . . . . 384
Suppressing Node Construction with Nothing . . . . . . . . . . . . . 385
Handling Multiple Peer Nodes While Remaining Flat . . . . . . . 388
Validation. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 391
The Extension Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 391
Declarations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 391
Obtaining an XML Schema . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 393
Examples . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 395

XPath . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 406
Declarations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 406
Examples . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 408
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 409

PART 4

NNN

CHAPTER 10

LINQ to DataSet

LINQ to DataSet Operators. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 413
Assembly References . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 414
Referenced Namespaces . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 414
Common Code for the Examples. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 414

www.it-ebooks.info
16445fm_CMP2.indd 15

7/17/09 10:51:54 AM


xvi

NCO NTENT S

DataRow Set Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 416
Distinct . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 416

Declarations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 416
Examples . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 417
Except . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 420
Declarations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 421
Examples . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 421
Intersect . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 423
Declarations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 423
Examples . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 423
Union . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 425
Declarations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 425
Examples . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 426
SequenceEqual . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 427
Declarations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 428
Examples . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 428
DataRow Field Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 429
Field(Of T) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 435
Declarations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 435
Examples . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 436
SetField(Of T) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 440
Declarations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 440
Examples . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 441
DataTable Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 444
AsEnumerable. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 444
Declarations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 444
Examples . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 444
CopyToDataTable(Of DataRow) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 445
Declarations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 445
Examples . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 446
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 451


CHAPTER 11

Additional DataSet Capabilities . . . . . . . . . . . . . . . . . . . . . . . . . . . 453
Required Namespaces . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 453
Typed DataSets . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 453
Putting It All Together . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 455
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 458

www.it-ebooks.info
16445fm_CMP2.indd 16

7/17/09 10:51:54 AM


NC O N T E N T S

PART 5

NNN

CHAPTER 12

xvii

LINQ to SQL

LINQ to SQL Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 463
Introducing LINQ to SQL . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 464
The DataContext . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 466
Entity Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 466

Associations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 467
Concurrency Conflict Detection . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 468
Concurrency Conflict Resolution . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 468
Prerequisites for Running the Examples . . . . . . . . . . . . . . . . . . . . . . . . . . . 469
Obtaining the Appropriate Version of the Northwind Database . . . 469
Generating the Northwind Entity Classes . . . . . . . . . . . . . . . . . . . . . . 469
Generating the Northwind XML Mapping File . . . . . . . . . . . . . . . . . . 471
Importing the Generated Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 471
Using the LINQ to SQL API . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 472
IQueryable(Of T) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 472
Some Common Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 473
GetStringFromDb() . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 473
ExecuteStatementInDb() . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 474
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 474

CHAPTER 13

LINQ to SQL Tips and Tools . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 477
Introduction to LINQ to SQL Tips and Tools. . . . . . . . . . . . . . . . . . . . . . . . . 477
Tips . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 478
Use the DataContext.Log Property . . . . . . . . . . . . . . . . . . . . . . . . . . . 478
Use the GetChangeSet() Method . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 479
Consider Using Partial Classes or Mapping Files . . . . . . . . . . . . . . . 479
Consider Using Partial Methods. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 480
Tools . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 480
SqlMetal . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 480
XML Mapping File vs. DBML Intermediate File . . . . . . . . . . . . 485
Working with DBML Intermediate Files . . . . . . . . . . . . . . . . . . 486
The Object Relational Designer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 487
Creating Your LINQ to SQL Classes File . . . . . . . . . . . . . . . . . . 487

Connecting the DataContext to the Database . . . . . . . . . . . . . 489
Adding an Entity Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 490
Editing the Entity Class Model . . . . . . . . . . . . . . . . . . . . . . . . . . 493

www.it-ebooks.info
16445fm_CMP2.indd 17

7/17/09 10:51:54 AM


xviii

NCO NTENT S

Adding Objects to the Entity Class Model. . . . . . . . . . . . . . . . . 494
Overriding the Insert, Update, and Delete Methods . . . . . . . . 495
Use SqlMetal and the O/R Designer Together . . . . . . . . . . . . . . . . . . . . . . 502
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 503

CHAPTER 14

LINQ to SQL Database Operations . . . . . . . . . . . . . . . . . . . . . . . . . 505
Prerequisites for Running the Examples . . . . . . . . . . . . . . . . . . . . . . . . . . . 505
Some Common Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 505
Using the LINQ to SQL API . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 506
Standard Database Operations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 506
Inserts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 506
Inserting Attached Entity Objects . . . . . . . . . . . . . . . . . . . . . . . . 509
Queries. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 511
Exceptions to the Norm . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 512

Associations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 514
Joins . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 527
Deferred Query Execution . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 532
The SQL IN Statement with the Contains Operator. . . . . . . . . 536
Updates . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 537
Updating Associated Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . 537
Deletes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 541
Deleting Attached Entity Objects . . . . . . . . . . . . . . . . . . . . . . . . 543
Deleting Relationships . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 543
Overriding Database Modification Statements . . . . . . . . . . . . . . . . . . . . . . 544
Overriding the Insert Method . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 545
Overriding the Update Method . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 545
Overriding the Delete Method . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 545
Example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 545
Overriding in the Object Relational Designer. . . . . . . . . . . . . . . . . . . 548
Considerations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 548
SQL Translation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 549
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 551

CHAPTER 15

LINQ to SQL Entity Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 553
Prerequisites for Running the Examples . . . . . . . . . . . . . . . . . . . . . . . . . . . 553
Entity Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 553
Creating Entity Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 553
Generating Entity Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 554
Writing Entity Classes By Hand . . . . . . . . . . . . . . . . . . . . . . . . . 554
Entity Class Attributes and Attribute Properties . . . . . . . . . . . 568
www.it-ebooks.info


16445fm_CMP2.indd 18

7/17/09 10:51:54 AM


NC O N T E N T S

xix

XML External Mapping File Schema . . . . . . . . . . . . . . . . . . . . . . . . . . 583
Projecting into Entity Classes vs. Nonentity Classes . . . . . . . . . . . . 583
Prefer Object Initialization to Parameterized
Construction When Projecting . . . . . . . . . . . . . . . . . . . . . . . . 586
Extending Entity Classes with Partial Methods . . . . . . . . . . . . . . . . . . . . . . 589
Important System.Data.Linq API Classes. . . . . . . . . . . . . . . . . . . . . . . . . . . 591
EntitySet(Of T) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 591
EntityRef(Of T) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 592
Entity. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 592
HasLoadedOrAssignedValue . . . . . . . . . . . . . . . . . . . . . . . . . . . . 593
Table(Of T) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 594
IExecuteResult . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 594
ReturnValue . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 595
GetParameterValue . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 595
ISingleResult(Of T) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 595
ReturnValue . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 596
IMultipleResults . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 596
ReturnValue . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 597
GetResult(Of T) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 597
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 598


CHAPTER 16

The DataContext

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 599

Prerequisites for Running the Examples . . . . . . . . . . . . . . . . . . . . . . . . . . . 599
Some Common Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 599
Using the LINQ to SQL API . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 599
[Your]DataContext Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 599
The DataContext Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 600
The DataContext Class Implements IDisposable . . . . . . . . . . . . . . . 603
Primary Purposes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 603
Identity Tracking . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 603
Change Tracking . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 609
Change Processing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 610
The DataContext Lifetime . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 610
DataContext() and [Your]DataContext() . . . . . . . . . . . . . . . . . . . . . . . . 611
Declarations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 611
Examples . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 612
Creating Mapping Sources . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 624
SubmitChanges() . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 624
Declarations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 624
Examples . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 625
www.it-ebooks.info
16445fm_CMP2.indd 19

7/17/09 10:51:54 AM



xx

NCO NTENT S

DatabaseExists() . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 631
Declarations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 632
Examples . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 632
CreateDatabase() . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 632
Declarations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 633
Examples . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 633
DeleteDatabase() . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 634
Declarations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 634
Examples . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 634
CreateMethodCallQuery() . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 635
Declarations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 635
Examples . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 635
ExecuteQuery() . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 637
Declarations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 637
Examples . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 637
Translate() . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 639
Declarations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 639
Examples . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 640
ExecuteCommand() . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 641
Declarations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 641
Examples . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 641
ExecuteMethodCall() . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 642
Declarations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 642
Examples . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 643
GetCommand() . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 650
Declarations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 650

Examples . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 651
GetChangeSet(). . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 652
Declarations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 652
Examples . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 652
GetTable() . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 654
Declarations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 654
Examples . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 654
Refresh() . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 655
Declarations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 655
Examples . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 656
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 662

www.it-ebooks.info
16445fm_CMP2.indd 20

7/17/09 10:51:54 AM


NC O N T E N T S

CHAPTER 17

xxi

Concurrency Conflicts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 663
Prerequisites for Running the Examples . . . . . . . . . . . . . . . . . . . . . . . . . . . 663
Some Common Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 663
Using the LINQ to SQL API . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 663
Concurrency Conflicts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 663
Optimistic Concurrency . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 664

Conflict Detection . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 664
Conflict Resolution . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 668
Pessimistic Concurrency. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 675
An Alternative Approach for N-Tier Architectures . . . . . . . . . . . . . . 678
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 680

CHAPTER 18

Additional SQL Capabilities

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 681

Prerequisites for Running the Examples . . . . . . . . . . . . . . . . . . . . . . . . . . . 681
Using the LINQ to SQL API . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 681
Using the LINQ to XML API . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 681
Database Views . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 681
Entity Class Inheritance . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 683
Transactions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 689
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 691

INDEX . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 695

www.it-ebooks.info
16445fm_CMP2.indd 21

7/17/09 10:51:54 AM


www.it-ebooks.info
16445fm_CMP2.indd 22


7/17/09 10:51:54 AM


About the Author
NJOSEPH 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 maintainable 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’s first book, Pro LINQ: Language Integrated Query in C# 2008, was published on
November 19, 2007, which is the exact same date that Visual Studio 2008 was released to manufacturing. One final LINQ coincidence is that Visual Studio 2008 was officially launched on
February 27, 2008, which just happens to be Joe’s birthday.
Joe can be reached at his web site, sss*hejm`ar*_ki.

xxiii


www.it-ebooks.info
16445fm_CMP2.indd 23

7/17/09 10:51:54 AM


Tài liệu bạn tìm kiếm đã sẵn sàng tải về

Tải bản đầy đủ ngay
×