cyan yelloW MaGenTa Black panTone 123 c Books for professionals By professionals® The eXperT’s Voice® in .neT Companion eBook Available Pro LINQ: Language Integrated Query in C# 2008 Dear Reader, Pro Pro 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 L 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 I intended and reap the maximum rewards for your investment. LINQ N 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, Q 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. QL This book is for anyone with an elementary understanding of C# who wants a to understand LINQ and LINQ-relevant C# 3.0 language features. You need not u n be up on all the latest C# 2.0 or 3.0 features to understand Pro LINQ. When a e deeper knowledge of an advanced language feature is necessary, I begin from rg netbooks.wordpress.com the ground up to make sure everyone is well equipped for the discussion. yu Joseph C. Rattz, Jr. ia Language Integrated Query ng e C THE APRESS ROADMAP in C# 2008 #I Accelerated C# 2008 n 2t Pro C# 2008 and the Silverlight and .NET 3.5 e Beginning C# 2008 0 .NET 3.5 Platform Recipes in C# 2008 g Learn to use the power of Microsoft’s Companion eBook 0 Pro LINQ: Language r ground-breaking new technology. Beginning Pro .NET 3.5 Scalable 8 Integrated Query a C# 2008 Databases Application Design in C# 2008 t Codered @ Updatesofts.com e Beginning ASP.NET 3.5 Expert Service-Oriented Data Access, 2e Pro WPF in C# 2008, 2e Architecture, 3e d See last page for details on $10 eBook version Illustrated C# 2008 Joseph C. Rattz, Jr. SOURCE CODE ONLINE ISBN-13: 978-1-59059-789-7 www.apress.com ISBN-10: 1-59059-789-3 54499 Rattz US $44.99 Shelve in Programming/C# User level: 9 781590 597897 Intermediate–Advanced 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 [email protected], or visit http://www.springeronline.com. 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 [email protected], 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 http://www.apress.com. 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-3FRONT.fm Page iv Thursday, October 25, 2007 8:59 AM Rattz_789-3FRONT.fm Page v Thursday, October 25, 2007 8:59 AM Contents at a Glance About the Author . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .xvii About the Technical Reviewer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xix Acknowledgments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxi PART 1 Pro LINQ: Language Integrated ■ ■ ■ Query in C# 2008 ■CHAPTER 1 Hello LINQ . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3 ■CHAPTER 2 C# 3.0 Language Enhancements for LINQ . . . . . . . . . . . . . . . . . . . . . 19 PART 2 LINQ to Objects ■ ■ ■ ■CHAPTER 3 LINQ to Objects Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 53 ■CHAPTER 4 Deferred Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 63 ■CHAPTER 5 Nondeferred Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 133 PART 3 LINQ to XML ■ ■ ■ ■CHAPTER 6 LINQ to XML Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 187 ■CHAPTER 7 The LINQ to XML API . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 193 ■CHAPTER 8 LINQ to XML Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 273 ■CHAPTER 9 Additional XML Capabilities . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 301 PART 4 LINQ to DataSet ■ ■ ■ ■CHAPTER 10 LINQ to DataSet Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 339 ■CHAPTER 11 Additional DataSet Capabilities . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 373 v Rattz_789-3FRONT.fm Page vi Thursday, October 25, 2007 8:59 AM PART 5 LINQ to SQL ■ ■ ■ ■CHAPTER 12 LINQ to SQL Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 381 ■CHAPTER 13 LINQ to SQL Tips and Tools . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 391 ■CHAPTER 14 LINQ to SQL Database Operations . . . . . . . . . . . . . . . . . . . . . . . . . . . . 415 ■CHAPTER 15 LINQ to SQL Entity Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 457 ■CHAPTER 16 The DataContext . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 499 ■CHAPTER 17 Concurrency Conflicts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 557 ■CHAPTER 18 Additional SQL Capabilities . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 573 ■INDEX . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 585 vi Rattz_789-3FRONT.fm Page vii Thursday, October 25, 2007 8:59 AM Contents About the Author . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .xvii About the Technical Reviewer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xix Acknowledgments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxi PART 1 Pro LINQ: Language Integrated ■ ■ ■ Query in C# 2008 ■CHAPTER 1 Hello LINQ . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3 A Paradigm Shift . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3 Query XML. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4 Query a SQL Server Database. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6 LINQ Is About Data Queries. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6 Components . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7 How to Obtain LINQ . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8 LINQ Is Not Just for Queries . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8 Tips to Get You Started . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11 Use the var Keyword When Confused . . . . . . . . . . . . . . . . . . . . . . . . 11 Use the Cast or OfType Operators for Legacy Collections . . . . . . . . 12 Prefer the OfType Operator to the Cast Operator . . . . . . . . . . . . . . . 13 Don’t Assume a Query Is Bug-Free . . . . . . . . . . . . . . . . . . . . . . . . . . 14 Take Advantage of Deferred Queries . . . . . . . . . . . . . . . . . . . . . . . . . 15 Use the DataContext Log. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15 Use the LINQ Forum. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16 vii Rattz_789-3FRONT.fm Page viii Thursday, October 25, 2007 8:59 AM viii ■CONTENTS ■CHAPTER 2 C# 3.0 Language Enhancements for LINQ . . . . . . . . . . . . . . . . 19 New C# 3.0 Language Additions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19 Lambda Expressions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19 Expression Trees. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24 Keyword var, Object Initialization, and Anonymous Types. . . . . . . . 25 Extension Methods. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 30 Partial Methods. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34 Query Expressions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 37 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 50 PART 2 LINQ to Objects ■ ■ ■ ■CHAPTER 3 LINQ to Objects Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 53 LINQ to Objects Overview . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 53 IEnumerable<T>, Sequences, and the Standard Query Operators . . . . . 54 Returning IEnumerable<T>, Yielding, and Deferred Queries . . . . . . . . . 55 Func Delegates . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 58 The Standard Query Operators Alphabetical Cross-Reference . . . . . . . . 59 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 61 ■CHAPTER 4 Deferred Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 63 Referenced Namespaces . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 63 Referenced Assemblies . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 63 Common Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 63 The Deferred Operators by Purpose . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 65 Restriction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 65 Projection. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 67 Partitioning . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 76 Concatenation. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 83 Ordering. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 86 Join. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 102 Grouping . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 106 Set . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 111 Conversion. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 117 Element . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 123 Generation. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 128 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 131
Description: