Pro Linq to SQL
Freeman Rattz, Jr. Companion eBook Available LINQ in C# 2010 Pro 7.5 x 9.25 spine = 1.5625" 840 page count THE EXPERT’S VOICE ® IN .NET Pro Language Integrated Query in C# 2010 Adam Freeman and Joseph C. Rattz, Jr. LINQ this print for content only—size & color not accurate 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 US $44.99 Shelve in: .NET User level: Intermediate–Advanced THE APRESS ROADMAP Pro ASP.NET MVC 2 Pro Silverlight Pro LINQ Pro C# 2010 and the .NET 4 Platform Pro ASP.NET 4 in C# 2010 Introducing .NET 4.0 Accelerated C# 2010 www.apress.com SOURCE CODE ONLINE Companion eBook See last page for details on $10 eBook version ISBN 978-1-4302-2653-6 9 781430 226536 5 44 9 9 Pro LINQ: Language Integrated Query in C# 2010 Dear Reader, Pro LINQ: Language Integrated Query in C# 2010 is all about code. The book starts with code, ends with code and every chapter is packed with code through- out. We set out to write a book that is a treasury of meaningful LINQ examples. We’ve tried to paint 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 get the full benefit from this powerful and flexible tool. We aim to give you the information you really need in a form that you can actually use. So, rather than obscure the LINQ principles by focusing on a com- plex demonstration application you can put to any practical use, Pro LINQ cuts right to the chase for each LINQ operator, method and class, telling you what you need to know—clearly and concisely. 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 the LINQ-related C# 4.0 language features. You don’t need to be up to speed on all the new .NET 4.0 features to understand Pro LINQ— when a deeper knowledge of an advanced language feature is required, we begin from the ground up to make sure everyone is equipped for the discussion. Adam Freeman and Joseph C. Rattz, Jr. Adam Freeman, Author of Programming .NET Security Microsoft .NET XML Web Services Step by Step C# for Java Developers www.free-ebooks-library.com www.free-ebooks-library.com Pro LINQ Language Integrated Query in C# 2010 ■ ■ ■ Adam Freeman and Joseph C. Rattz, Jr. www.free-ebooks-library.com ■ CONTENTS ii Pro LINQ: Lan gua ge Integrated Que ry in C# 2010 Copyright © 2010 by Adam Freeman and 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-4302-2653-6 ISBN-13 (electronic): 978-1-4302-2654-3 Printed and bound in the United States of America 9 8 7 6 5 4 3 2 1 Trademarked names, logos, and images may appear in this book. Rather than use a trademark symbol with every occurrence of a trademarked name, logo, or image we use the names, logos, and images only in an editorial fashion and to the benefit of the trademark owner, with no intention of infringement of the trademark. The use in this publication of trade names, trademarks, service marks, and similar terms, even if they are not identified as such, is not to be taken as an expression of opinion as to whether or not they are subject to proprietary rights. President and Publisher: Paul Manning Lead Editor: Ewan Buckingham Technical Reviewer: Fabio Claudio Ferracchiati Editorial Board: Clay Andres, Steve Anglin, Mark Beckner, Ewan Buckingham, Gary Cornell, Jonathan Gennick, Jonathan Hassell, Michelle Lowman, Matthew Moodie, Duncan Parkes, Jeffrey Pepper, Frank Pohlmann, Douglas Pundick, Ben Renow-Clarke, Dominic Shakeshaft, Matt Wade, Tom Welsh Coordinating Editor: Mary Tobin Copy Editor: Kim Wimpsett Compositor: Bronkella Publishing LLC Indexer: BIM Indexing & Proofreading Services Artist: April Milne Cover Designer: Anna Ishchenko Distributed to the book trade worldwide by Springer Science+Business Media, LLC., 233 Spring Street, 6th Floor, New York, NY 10013. Phone 1-800-SPRINGER, fax (201) 348-4505, e-mail orders- ny@springer-sbm.com, or visit www.springeronline.com. For information on translations, please e-mail rights@apress.com, or visit www.apress.com. 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 www.apress.com/info/bulksales. 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 www.apress.com. You will need to answer questions pertaining to this book in order to successfully download the code. ■ CONTENTS iii Dedicated to my wife, Jacqui Griffyth —Adam Freeman For my parents, Joe and Josie Rattz, that believed I could do anything and encouraged me to believe the same. Thank you. —Joseph C. Rattz, Jr. ■ CONTENTS iv Contents at a Glance Contents at a Glan ce iv Contents . vi About the Author . xx About the Technical Reviewer . xxi Acknowledgments . xxii Part 1: Pro LINQ: Language Integrated Qu ery in C# 2010 .1 ■Chapter 1: H ello LINQ . 3 ■Chapter 2: C # Language Enhancement s for LINQ . 21 Part 2: LINQ to Objects 55 ■Chapter 3: LINQ to Objects Introduct ion 57 ■Chapter 4: D eferred Operators . 69 ■Chapter 5: Nondeferr ed Opera tors 151 Part 3: LINQ to XM L . 211 ■Chapter 6: LINQ to X ML Introduction 213 ■Chapter 7: The LINQ to XML API . 219 ■Chapter 8: LINQ to X ML Operators 313 ■Chapter 9: A dditional XML C apabilities . 345 Part 4: LINQ to DataSet 387 ■Chapter 10: LINQ to D ataSet Operat ors . 389 ■Chapter 11: Additional DataSet Capabilities 427 Part 5: LINQ to SQL 435 ■Chapter 12: LINQ to SQL Intr oduction . 437 ■ CONTENTS v ■Chapter 13: LINQ to SQL Tips and Tools 449 ■Chapter 14: LINQ to SQL Dat abase Operati ons . 475 ■Chapter 15: LINQ to SQL Ent ity Classes 521 ■Chapter 16: The LINQ to SQL DataContext . 569 ■Chapter 17: LINQ to SQL Concurrency Conf licts . 635 ■Chapter 18: Additional LINQ to SQL Capabilities 653 Part 6: LINQ to Ent ities 665 ■Chapter 19: LINQ to Entities Introduction 667 ■Chapter 20: LINQ to Entities Operations 679 ■Chapter 21: LINQ to Entities Classes 723 Part 7: Par allel LINQ .749 ■Chapter 22: Parallel LINQ Introduction . 751 ■Chapter 23: U sing Parallel LINQ . 757 ■Chapter 24: Parallel LINQ Operators . 773 Index . 795 ■ CONTENTS vi Contents Contents at a Glan ce iv Contents . vi About the Author . xx About the Technical Reviewer . xxi Acknowledgments . xxii Part 1: Pro LINQ: Language Integrated Qu ery in C# 2010 .1 ■Chapter 1: H ello LINQ . 3 A Paradigm Shift . 3 Query XML . 4 Query a SQL Server Database 5 Introduction . 7 LINQ Is About Data Queries 7 How to Obtain LINQ 8 LINQ Is Not Just for Queries 8 Tips to Get You Started . 12 Use the var Keyword When Confused 12 Use the Cast or OfType Operators for Legacy Collections . 14 The OfType Operator versus the Cast Operator . 15 Don’t Assume a Query Is Bug-Free 15 Take Advantage of Deferred Queries . 16 Use the DataContext Log . 17 Use the LINQ Forum . 18 Summary 18 ■ CONTENTS vii ■Chapter 2: C # Language Enhancement s for LINQ . 21 C# Language Additions . 21 Lambda Expressions 22 Expression Trees . 27 Keyword var, Object Initialization, and Anonymous Types 28 Extension Methods 33 Partial Methods . 39 Query Expressions . 41 Summary 52 Part 2: LINQ to Objects 55 ■Chapter 3: LINQ to Objects Introduct ion 57 LINQ to Objects Overview . 57 IEnumerable<T>, Sequences, and the Standard Query Operators . 58 Returning IEnumerable<T>, Yielding, and Deferred Queries . 59 Func Delegates . 62 The Standard Query Operators Alphabetical Cross-Reference . 64 A Tale of Two Syntaxes . 66 Summary 66 ■Chapter 4: D eferred Operators . 69 Referenced Namespaces 69 Referenced Assemblies 69 Common Classes 69 The Deferred Operators by Purpose . 71 Restriction . 71 Projection . 74 Partitioning 85 Concatenation 93 Ordering . 96 Join 116 ■ CONTENTS viii Grouping 120 Set . 126 Conversion . 133 Element 140 Generation . 145 Summary 148 ■Chapter 5: Nondeferr ed Opera tors 151 Referenced Namespaces 151 Common Classes 151 The Nondeferred Operators by Purpose . 154 Conversion . 155 Equality 168 Element 172 Quantifiers . 187 Aggregate 193 Summary 209 Part 3: LINQ to XM L . 211 ■Chapter 6: LINQ to X ML Introduction 213 Introduction . 215 Cheating the W3C DOM XML API . 216 Summary 217 ■Chapter 7: The LINQ to XML API . 219 Referenced Namespaces 219 Significant API Design Enhancements 219 XML Tree Construction Simplified with Functional Construction 220 Document Centricity Eliminated in Favor of Element Centricity 222 Names, Namespaces, and Prefixes . 224 Node Value Extraction . 227 The LINQ to XML Object Model . 230