CYAN
MAGENTA
YELLOW
BLACK
PANTONE 123 C
Books for professionals by professionals ®
Pro LINQ: Language Integrated Query in C# 2008
Dear Reader,
Joseph C. Rattz, Jr.
THE APRESS ROADMAP
Accelerated C# 2008
Beginning C# 2008
Pro C# 2008 and the
.NET 3.5 Platform
Silverlight and .NET 3.5
Recipes in C# 2008
Beginning
C# 2008 Databases
Pro LINQ: Language
Integrated Query
in C# 2008
Pro .NET 3.5 Scalable
Application Design
Beginning ASP.NET 3.5
Data Access, 2e
Pro WPF in C# 2008, 2e
Expert Service-Oriented
Architecture, 3e
Companion eBook
See last page for details
on $10 eBook version
Companion
eBook Available
Pro
Language Integrated
in C# 2008
LINQ Query
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 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 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.
The EXPERT’s VOIce ® in .NET
Pro
LINQ
Language Integrated Query
in C# 2008
Learn to use the power of Microsoft’s
ground-breaking new technology.
Illustrated C# 2008
SOURCE CODE ONLINE
www.apress.com
Joseph C. Rattz, Jr.
ISBN-13: 978-1-59059-789-7
ISBN-10: 1-59059-789-3
54499
US $44.99
Rattz
Shelve in
Programming/C#
User level:
Intermediate–Advanced
9 781590 597897
this print for content only—size & color not accurate
spine = 1.176" 624 page count
Rattz_789-3FRONT.fm Page i Thursday, October 25, 2007 8:59 AM
Pro LINQ
Language Integrated Query
in C# 2008
■■■
Joseph C. Rattz, Jr.
Rattz_789-3FRONT.fm Page ii 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 iii 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-3INDEX.fm Page 599 Wednesday, October 31, 2007 3:06 PM
■I N D E X
XComment.Value, on XComment objects, 250
XElement.SetAttributeValue() method, 262–263
XContainer class, creating containers with, 210
XElement.SetElementValue() method, 253–255
XContainer.Add() (AddLast) method, 243
XElement.Value, on XElement objects 250
XContainer.AddFirst() method, 243–244
XElement.XElement(XName name, object
content); 206
XContainer.Descendants() method, traversing
236–237
XContainer.Elements() method, 232–234
vs. Extensions.Elements operator, 273–274
XLinq. See LINQ to XML
XML
additional capabilities, 301–336
XContainer.Nodes() method, 228–229
desired structure, 188
XDeclaration class, 210–211
LINQ to, 187–192
XDocument class, 212–213
querying with XPath syntax, 335
XDocument.Load() method, 220–222
simple example, 187–188
XML annotations, 263–267
XDocumentType class, 211–212
XML attributes, 255–263
XDocumentType.InteralSubset, 251
XML data
XDocumentType.Name, 251
modifying, 242–253
XDocumentType.PublicId, 251
query joining database data with, 583
XElement class, 4
XElement object
accessing XML document from, 226–227
instantiating, 206
traversing down recursively from, 236–237
traversing up from, 227–228, 235–236
XElement.AddFirst method, 215
XElement.AncestorsAndSelf() method, 235–236
XElement.Attribute() method, 257–258
XElement.Attributes() method, 258
XElement.DescendantsAndSelf() method,
237–238
XML documents. See also documents
allowing user edit of book participant,
331–332
creating for validating, 330–331
creating new for transformed version, 311
creating with book participants, 311
transforming using function construction,
312–314
transforming with XSLT, 310–311
unsuccessfully validating against XSD
schema, 325–329
unsuccessfully validating XML element,
332–334
validating, 322–325, 331
XElement.FirstAttribute property, 255
XML element, successfully validating, 330
XElement.LastAttribute property, 257
XML events
XElement.Load() method, 222–223
examples, 268–272
XElement.RemoveAll() method, 249
registering for, 267
XElement.ReplaceAll() method, 252–253
XML external mapping, file schema, 484–485
XElement.Save() methods, 219–220
XML input, 220–222
Find it faster at
XDocument.Save() methods, 218–219
XDocumentType.SystemId, 251
599
Rattz_789-3INDEX.fm Page 600 Wednesday, October 31, 2007 3:06 PM
600
■I N D E X
XML mapping files
XNamespace class, 214
abbreviated external, 517–518
XNode.AddAfterSelf() method, 246
considerations for using, 393
XNode.AddBeforeSelf() method, 244–245
vs. DBML intermediate file, 398
XNode.Ancestors() method, 234–235
XML namespaces. See namespaces
XNode.ElementsAfterSelf() method, 239–240
XML output, 218–220
XNode.ElementsBeforeSelf() method, 241–242
XML schema
XNode.NextNode property, 225
creating, 321–322
XNode.NodesAfterSelf() method, 238–239
creating using functional construction,
194–195
XNode.NodesBeforeSelf() method, 240–241
obtaining, 321–334
XML strings, parsing, 223
XML transformations. See also transformations
simplifying with helper methods, 314
tips for performing, 314–319
XML traversal, 224–242
properties, 225–228
XML tree
XNode.PreviousNode property, 225–226
XNode.Remove() method247–248
XNodeDocumentOrderComparer class, 202
XNodeEqualityComparer class, 202
XObject event handling, 269–272
XObject.AddAnnotation() method, 263
XObject.Annotation()/XObject.Annotations()
method, 263
XObject.Changed event, 268
adding nodes to, 242–243
XObject.Changing event, 268
for examples in LINQ to XML, 312
XObject.Document property, accessing XML
document via, 226–227
generating with a LINQ query, 207–208
problems removing nodes from, 203–205
producing, 200
recursively traversing up, 234–235
traversing backward, 225–226
traversing down, 228–229
traversing forward through, 225
XML tree construction
deferred execution of, 217
immediate execution of, 216
XObject.Parent property, 227–228
XObject.RemoveAnnotations() method, 264
XProcessingInstruction class, 214–215
XProcessingInstruction.Data, 252
XProcessingInstruction.Target, 252
XSLT trasformations, advantages of, 309–310
XStreamingElement class, 216–217
XText object, creating text node with, 217–218
XText.Value, on XText objects, 250
XMLHelper method, 191
■Y
XmlMappingSource object, 518
yield keyword, for writing enumerators, 55
XName object
[Your]DataContext class, 499–500
creating, 213
for XML elements, 198–199
constructor prototype connecting to
database, 511–512