Paolo Pialorsi - Pearsoncmg

Transcription

Programming MicrosoftLINQ in Microsoft .NETFramework 4 Paolo PialorsiMarco Russo

Copyright 2010 by Paolo Pialorsi and Marco RussoComplying with all applicable copyright laws is the responsibility of the user. All rights reserved. Without limiting therights under copyright, no part of this document may be reproduced, stored in or introduced into a retrieval system, ortransmitted in any form or by any means (electronic, mechanical, photocopying, recording, or otherwise), or for anypurpose, without express written permission of Microsoft Press, Inc.Printed and bound in the United States of America.123456789 M 543210Microsoft Press titles may be purchased for educational, business or sales promotional use. Online editions are alsoavailable for most titles (http://my.safaribooksonline.com). For more information, contact our corporate/institutionalsales department: (800) 998-9938 or send comments to mspinput@microsoft.com.Microsoft, Microsoft Press, ActiveX, Excel, FrontPage, Internet Explorer, PowerPoint, SharePoint, Webdings, Windows,and Windows 7 are either registered trademarks or trademarks of Microsoft Corporation in the United States and/orother countries. Other product and company names mentioned herein may be the trademarks of their respective owners.Unless otherwise noted, the example companies, organizations, products, domain names, e-mail addresses, logos, people,places, and events depicted herein are fictitious, and no association with any real company, organization, product, domainname, e-mail address, logo, person, place, or event is intended or should be inferred.This book expresses the author’s views and opinions. The information contained in this book is provided without anyexpress, statutory, or implied warranties. Neither the author, Microsoft Corporation, nor theirrespective resellers or distributors, will be held liable for any damages caused or alleged to be caused either directly orindirectly by such information.Acquisitions and Development Editor: Russell JonesProduction Editor: Adam ZarembaEditorial Production: OTSI, Inc.Technical Reviewer: Debbie TimminsIndexing: Ron StraussCover: Karen MontgomeryCompositor: Octal Publishing, Inc.Illustrator: Robert Romano978-0-735-64057-3

To Andrea and Paola: thanks for your everyday support!—Paolo

Contents at a GlancePart I1LINQ FoundationsLINQ Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 32LINQ Syntax Fundamentals . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 233LINQ to Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 49Part II LINQ to Relational4 Choosing Between LINQ to SQL and LINQ to Entities . . . . . . . . . . . . 1115LINQ to SQL: Querying Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1196LINQ to SQL: Managing Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1717LINQ to SQL: Modeling Data and Tools . . . . . . . . . . . . . . . . . . . . . . . . 2058LINQ to Entities: Modeling Data with Entity Framework . . . . . . . . . . 2419LINQ to Entities: Querying Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27310LINQ to Entities: Managing Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 30111LINQ to DataSet . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 343Part III LINQ to XML12 LINQ to XML: Managing the XML Infoset . . . . . . . . . . . . . . . . . . . . . . 35913LINQ to XML: Querying Nodes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 385Part IV Advanced LINQ14 Inside Expression Trees . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 41515Extending LINQ . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 46516Parallelism and Asynchronous Processing . . . . . . . . . . . . . . . . . . . . . . 51717Other LINQ Implementations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 563Part V Applied LINQ18 LINQ in a Multitier Solution . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 57719LINQ Data Binding . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 609v

Table of ContentsPreface . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xviiAcknowledgments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xixIntroduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxiPart I1LINQ FoundationsLINQ Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3What Is LINQ? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3Why Do We Need LINQ? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5How LINQ Works . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6Relational Model vs. Hierarchical/Network Model . . . . . . . . . . . . . . . . . . . . 8XML Manipulation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14Language Integration . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17Declarative Programming . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17Type Checking . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19Transparency Across Different Type Systems . . . . . . . . . . . . . . . . . . . . . . . . 20LINQ Implementations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20LINQ to Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20LINQ to ADO.NET . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21LINQ to XML . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 222LINQ Syntax Fundamentals . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23LINQ Queries . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23Query Syntax . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23Full Query Syntax . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 28Query Keywords . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 29From Clause . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 29Where Clause . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 32Select Clause . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 32Group and Into Clauses . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33Orderby Clause . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35Join Clause . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 36Let Clause . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 40Additional Visual Basic Keywords . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 41What do you think of this book? We want to hear from you!Microsoft is interested in hearing your feedback so we can continually improve ourbooks and learning resources for you. To participate in a brief online survey, please visit:microsoft.com/learning/booksurveyvii

viiiTable of ContentsDeferred Query Evaluation and Extension Method Resolution . . . . . . . . . . . . . . 42Deferred Query Evaluation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 42Extension Method Resolution . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 43Some Final Thoughts About LINQ Queries . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 45Degenerate Query Expressions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 45Exception Handling . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 46Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 483LINQ to Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 49Query Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 53The Where Operator . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 53Projection Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 54Ordering Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 58Grouping Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 62Join Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 66Set Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 71Aggregate Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 77Aggregate Operators in Visual Basic . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 86Generation Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 88Quantifier Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 90Partitioning Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 92Element Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 95Other Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 100Conversion Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 101AsEnumerable . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 101ToArray and ToList . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 103ToDictionary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 104ToLookup . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 106OfType and Cast . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 107Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 108Part II LINQ to Relational4Choosing Between LINQ to SQL and LINQ to Entities . . . . . . . . . . . . 111Comparison Factors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .When to Choose LINQ to Entities and the Entity Framework . . . . . . . . . . . . . .When to Choose LINQ to SQL . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Other Considerations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .111112114116117

Table of Contents5LINQ to SQL: Querying Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 119Entities in LINQ to SQL . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 120External Mapping . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 122Data Modeling . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 124DataContext . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 124Entity Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 125Entity Inheritance . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 127Unique Object Identity . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 129Entity Constraints . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 130Associations Between Entities . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 130Relational Model vs. Hierarchical Model . . . . . . . . . . . . . . . . . . . . . . . . . . . 138Data Querying . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 138Projections . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 141Stored Procedures and User-Defined Functions . . . . . . . . . . . . . . . . . . . . 142Compiled Queries . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 150Different Approaches to Querying Data . . . . . . . . . . . . . . . . . . . . . . . . . . . 152Direct Queries . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 155Deferred Loading of Entities . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 157Deferred Loading of Properties . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 159Read-Only DataContext Access . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 161Limitations of LINQ to SQL . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 161Thinking in LINQ to SQL . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 163The IN/EXISTS Clause . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 163SQL Query Reduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 166Mixing .NET Code with SQL Queries . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 167Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1706LINQ to SQL: Managing Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 171CRUD and CUD Operations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Entity Updates . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Database Updates . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Customizing Insert, Update, and Delete . . . . . . . . . . . . . . . . . . . . . . . . . . . .Database Interaction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Concurrent Operations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Transactions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Exceptions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Databases and Entities . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Entity Attributes to Maintain Valid Relationships . . . . . . . . . . . . . . . . . . .Deriving Entity Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Attaching Entities . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Binding Metadata . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Differences Between the .NET Framework and SQL Type Systems . . . . .Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .171172179183185185189190192192194197201204204ix

xTable of Contents7LINQ to SQL: Modeling Data and Tools . . . . . . . . . . . . . . . . . . . . . . . . 205File Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .DBML—Database Markup Language . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .C# and Visual Basic Source Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .XML—External Mapping File . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .LINQ to SQL File Generation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .SQLMetal . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Generating a DBML File from a Database . . . . . . . . . . . . . . . . . . . . . . . . . .Generating Source Code and a Mapping File from a Database . . . . . . .Generating Source Code and a Mapping File from a DBML File . . . . . . .Using the Object Relational Designer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .DataContext Properties . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Entity Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Association Between Entities . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Entity Inheritance . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Stored Procedures and User-Defined Functions . . . . . . . . . . . . . . . . . . . .Views and Schema Support . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .8LINQ to Entities: Modeling Data with Entity Framework . . . . . . . . . . 241The Entity Data Model . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Generating a Model from an Existing Database . . . . . . . . . . . . . . . . . . . .Starting from an Empty Model . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Generated Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Entity Data Model (.edmx) Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Associations and Foreign Keys . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Complex Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Inheritance and Conditional Mapping . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Modeling Stored Procedures . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Non-CUD Stored Procedures . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .CUD Stored Procedures . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .POCO Support . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .T4 Templates . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 239241241244245248250254257259259262266271272LINQ to Entities: Querying Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 273EntityClient Managed Providers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .LINQ to Entities . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Selecting Single Entities . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Unsupported Methods and Keywords . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Canonical and Database Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .User-Defined Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Stored Procedures . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .273275277278279281283

Table of ContentsObjectQuery T and ObjectContext . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Lazy Loading . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Include . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Load and IsLoaded . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .The LoadProperty Method . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .MergeOption . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .The ToTraceString Method . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .ExecuteStoreCommand and ExecuteStoreQuery . . . . . . . . . . . . . . . . . . . . .The Translate T Method . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Query Performance . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Pre-Build Store Views . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .EnablePlanCaching . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Pre-Compiled Queries . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Tracking vs. No Tracking . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . NQ to Entities: Managing Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 301Managing Entities . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 301Adding a New Entity . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 301Updating an Entity . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 302Deleting an Entity . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 303Using SaveChanges . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 304Cascade Add/Update/Delete . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 305Managing Relationships . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 309Using ObjectStateManager and EntityState . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 311DetectChanges and AcceptAllChanges . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 313ChangeObjectState and ChangeRelationshipState . . . . . . . . . . . . . . . . . . . 314ObjectStateManagerChanged . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 315EntityKey . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 316GetObjectByKey and TryGetObjectByKey . . . . . . . . . . . . . . . . . . . . . . . . . . .317Managing Concurrency Conflicts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 319Managing Transactions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 322Detaching, Attaching, and Serializing Entities . . . . . . . . . . . . . . . . . . . . . . . . . . . 327Detaching Entities . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 327Attaching Entities . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 328ApplyOriginalValues and ApplyCurrentValues . . . . . . . . . . . . . . . . . . . . . . 330Serializing Entities . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 333Using Self-Tracking Entities . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 337Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 342xi

xiiTable of Contents11LINQ to DataSet . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 343Introducing LINQ to DataSet . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Using LINQ to Load a DataSet . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Loading a DataSet with LINQ to SQL . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Loading Data with LINQ to DataSet . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Using LINQ to Query a DataSet . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Understanding DataTable.AsEnumerable . . . . . . . . . . . . . . . . . . . . . . . . . .Creating DataView Instances with LINQ . . . . . . . . . . . . . . . . . . . . . . . . . . .Using LINQ to Query a Typed DataSet . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Accessing Untyped DataSet Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Comparing DataRow Instances . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .343344344346348350351352353353355Part III LINQ to XML12LINQ to XML: Managing the XML Infoset . . . . . . . . . . . . . . . . . . . . . . 359Introducing LINQ to XML . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .LINQ to XML Programming . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .XDocument . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .XElement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .XAttribute . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .XNode . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .XName and XNamespace . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Other X* Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .XStreamingElement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .XObject and Annotations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Reading, Traversing, and Modifying XML . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .13360363364365369370372377377379382384LINQ to XML: Querying Nodes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 385Querying XML . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Attribute, Attributes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Element, Elements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .XPath Axes “Like” Extension Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . .XNode Selection Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .InDocumentOrder . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Understanding Deferred Query Evaluation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Using LINQ Queries over XML . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Querying XML Efficiently to Build Entities . . . . . . . . . . . . . . . . . . . . . . . . .Transforming XML with LINQ to XML . . . . . . . . . . . . . . . . . . . . . . . . . . .

Microsoft Press titles may be purchased for educational, business or sales promotional use. Online editions are also . 3 LINQ to Objects . 4 Choosing Between LINQ to SQL and LINQ to Entities .111 5 LINQ to SQL: Querying Data.119 6 LINQ to SQL: Managing Data .