Programming Microsoft® LINQ in Microsoft .NET Framework 4
Total Page:16
File Type:pdf, Size:1020Kb
Programming Microsoft® LINQ in Microsoft .NET Framework 4 Paolo Pialorsi Marco Russo Copyright © 2010 by Paolo Pialorsi and Marco Russo Complying with all applicable copyright laws is the responsibility of the user. All rights reserved. Without limiting the rights under copyright, no part of this document may be reproduced, stored in or introduced into a retrieval system, or transmitted in any form or by any means (electronic, mechanical, photocopying, recording, or otherwise), or for any purpose, without express written permission of Microsoft Press, Inc. Printed and bound in the United States of America. 1 2 3 4 5 6 7 8 9 M 5 4 3 2 1 0 Microsoft Press titles may be purchased for educational, business or sales promotional use. Online editions are also available for most titles (http://my.safaribooksonline.com). For more information, contact our corporate/institutional sales department: (800) 998-9938 or send comments to [email protected]. 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/or other 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, domain name, 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 any express, statutory, or implied warranties. Neither the author, Microsoft Corporation, nor their respective resellers or distributors, will be held liable for any damages caused or alleged to be caused either directly or indirectly by such information. Acquisitions and Development Editor: Russell Jones Production Editor: Adam Zaremba Editorial Production: OTSI, Inc. Technical Reviewer: Debbie Timmins Indexing: Ron Strauss Cover: Karen Montgomery Compositor: Octal Publishing, Inc. Illustrator: Robert Romano 978-0-735-64057-3 To Andrea and Paola: thanks for your everyday support! —Paolo Contents at a Glance Part I LINQ Foundations 1 LINQ Introduction . 3 2 LINQ Syntax Fundamentals . 23 3 LINQ to Objects . 49 Part II LINQ to Relational 4 Choosing Between LINQ to SQL and LINQ to Entities . 111 5 LINQ to SQL: Querying Data . 119 6 LINQ to SQL: Managing Data . 171 7 LINQ to SQL: Modeling Data and Tools . 205 8 LINQ to Entities: Modeling Data with Entity Framework . 241 9 LINQ to Entities: Querying Data . 273 10 LINQ to Entities: Managing Data . 301 11 LINQ to DataSet . 343 Part III LINQ to XML 12 LINQ to XML: Managing the XML Infoset . 359 13 LINQ to XML: Querying Nodes . 385 Part IV Advanced LINQ 14 Inside Expression Trees . 415 15 Extending LINQ . 465 16 Parallelism and Asynchronous Processing . 517 17 Other LINQ Implementations . 563 Part V Applied LINQ 18 LINQ in a Multitier Solution . 577 19 LINQ Data Binding . 609 v Table of Contents Preface . xvii Acknowledgments . .xix Introduction . xxi Part I LINQ Foundations 1 LINQ Introduction . 3 What Is LINQ? . .3 Why Do We Need LINQ? . 5 How LINQ Works . 6 Relational Model vs. Hierarchical/Network Model . 8 XML Manipulation . .14 Language Integration . .17 Declarative Programming . 17 Type Checking . 19 Transparency Across Different Type Systems . 20 LINQ Implementations . 20 LINQ to Objects . 20 LINQ to ADO.NET . 21 LINQ to XML . 22 Summary . 22 2 LINQ Syntax Fundamentals . 23 LINQ Queries . .23 Query Syntax . .23 Full Query Syntax . 28 Query Keywords . 29 From Clause . 29 Where Clause . .32 Select Clause . 32 Group and Into Clauses . .33 Orderby Clause . 35 Join Clause . 36 Let Clause . 40 Additional Visual Basic Keywords . 41 What do you think of this book? We want to hear from you! Microsoft is interested in hearing your feedback so we can continually improve our books and learning resources for you. To participate in a brief online survey, please visit: microsoft.com/learning/booksurvey vii viii Table of Contents Deferred Query Evaluation and Extension Method Resolution . 42 Deferred Query Evaluation . .42 Extension Method Resolution . .43 Some Final Thoughts About LINQ Queries . .45 Degenerate Query Expressions . 45 Exception Handling . 46 Summary . 48 3 LINQ to Objects . 49 Query Operators . 53 The Where Operator . .53 Projection Operators . 54 Ordering Operators . 58 Grouping Operators . .62 Join Operators . 66 Set Operators . .71 Aggregate Operators . 77 Aggregate Operators in Visual Basic . 86 Generation Operators . 88 Quantifier Operators . 90 Partitioning Operators . .92 Element Operators . .95 Other Operators . 100 Conversion Operators . 101 AsEnumerable . 101 ToArray and ToList . 103 ToDictionary . 104 ToLookup . 106 OfType and Cast . 107 Summary . 108 Part II LINQ to Relational 4 Choosing Between LINQ to SQL and LINQ to Entities . 111 Comparison Factors . 111 When to Choose LINQ to Entities and the Entity Framework . 112 When to Choose LINQ to SQL . 114 Other Considerations . 116 Summary . 117 Table of Contents ix 5 LINQ to SQL: Querying Data . 119 Entities in LINQ to SQL . 120 External Mapping . 122 Data Modeling . 124 DataContext . 124 Entity Classes . 125 Entity Inheritance . 127 Unique Object Identity . 129 Entity Constraints . 130 Associations Between Entities . 130 Relational Model vs. Hierarchical Model . 138 Data Querying . 138 Projections . 141 Stored Procedures and User-Defined Functions . 142 Compiled Queries . 150 Different Approaches to Querying Data . 152 Direct Queries . 155 Deferred Loading of Entities . 157 Deferred Loading of Properties . 159 Read-Only DataContext Access . 161 Limitations of LINQ to SQL . 161 Thinking in LINQ to SQL . 163 The IN/EXISTS Clause . 163 SQL Query Reduction . 166 Mixing .NET Code with SQL Queries . 167 Summary . .170 6 LINQ to SQL: Managing Data . 171 CRUD and CUD Operations . 171 Entity Updates . 172 Database Updates . 179 Customizing Insert, Update, and Delete . 183 Database Interaction . 185 Concurrent Operations . 185 Transactions . 189 Exceptions . 190 Databases and Entities . 192 Entity Attributes to Maintain Valid Relationships . 192 Deriving Entity Classes . 194 Attaching Entities . ..