LINQ Pocket Reference LINQ Pocket Reference Joseph Albahari and Ben Albahari Beijing • Cambridge • Farnham • Köln • Paris • Sebastopol • Taipei • Tokyo LINQ Pocket Reference by Joseph Albahari and Ben Albahari Copyright © 2008 Joseph Albahari and Ben Albahari. All rights reserved. Printed in Canada. Published by O’Reilly Media, Inc., 1005 Gravenstein Highway North, Sebastopol, CA 95472. O’Reilly books may be purchased for educational, business, or sales promotional use. Online editions are also available for most titles (safari.oreilly.com). For more information, contact our corporate/ institutional sales department: (800) 998-9938 [email protected]. Editor: Laurel R.T. Ruma Cover Designer: Karen Montgomery Production Editor: Loranah Dimant Interior Designer: David Futato Proofreader: Loranah Dimant Illustrator: Robert Romano Indexer: Julie Hawks Printing History: February 2008: First Edition. NutshellHandbook,theNutshellHandbooklogo,andtheO’Reillylogoare registered trademarks of O’Reilly Media, Inc. ThePocket Reference series designations,LINQPocketReference,theimageofthehornedscreamer,and related trade dress are trademarks of O’Reilly Media, Inc. Many of the designations used by manufacturers and sellers to distinguish theirproductsareclaimedastrademarks.Wherethosedesignationsappear in this book, and O’Reilly Media, Inc. was aware of a trademark claim, the designations have been printed in caps or initial caps. .NET is a registered trademark of Microsoft Corporation. While every precaution has been taken in the preparation of this book, the publisher and authors assume no responsibility for errors or omissions, or for damages resulting from the use of the information contained herein. ISBN: 978-0-596-51924-7 [TM] Contents Getting Started 1 Lambda Queries 4 Chaining Query Operators 4 Composing Lambda Expressions 6 Natural Ordering 9 Other Operators 9 Comprehension Queries 10 Iteration Variables 12 Query Syntax Versus SQL Syntax 13 Query Syntax Versus Lambda Syntax 13 Mixed Syntax Queries 14 Deferred Execution 15 Reevaluation 16 Outer Variables 17 How Deferred Execution Works 17 Chaining Decorators 19 How Queries Are Executed 20 Subqueries 22 Subqueries and Deferred Execution 25 v Composition Strategies 25 Progressive Query Building 25 The into Keyword 27 Wrapping Queries 28 Projection Strategies 30 Object Initializers 30 Anonymous Types 30 The let Keyword 32 Interpreted Queries 33 How Interpreted Queries Work 35 AsEnumerable 38 LINQ to SQL 40 LINQ to SQL Entity Classes 40 DataContext 42 Automatic Entity Generation 45 Associations 45 Deferred Execution with LINQ to SQL 47 DataLoadOptions 48 Updates 50 Building Query Expressions 52 Delegates Versus Expression Trees 53 Expression Trees 55 Query Operator Overview 59 Filtering 62 Where 63 Take and Skip 65 TakeWhile and SkipWhile 65 Distinct 66 vi | Contents Projecting 66 Select 67 SelectMany 72 Joining 82 Join and GroupJoin 83 Ordering 92 OrderBy, OrderByDescending, ThenBy, ThenByDescending 92 Grouping 95 GroupBy 96 Set Operators 100 Concat and Union 100 Intersect and Except 100 Conversion Methods 101 OfType and Cast 101 ToArray, ToList, ToDictionary, ToLookup 103 AsEnumerable and AsQueryable 104 Element Operators 104 First, Last, Single 105 ElementAt 106 DefaultIfEmpty 107 Aggregation Methods 107 Count and LongCount 107 Min and Max 108 Sum and Average 109 Aggregate 110 Quantifiers 111 Contains and Any 111 All and SequenceEqual 112 Contents | vii Generation Methods 112 Empty 112 Range and Repeat 113 LINQ to XML 113 Architectural Overview 114 X-DOM Overview 115 Loading and Parsing 117 Saving and Serializing 118 Instantiating an X-DOM 118 Functional Construction 119 Specifying Content 120 Automatic Deep Cloning 121 Navigating/Querying an X-DOM 122 Child Node Navigation 122 Parent Navigation 126 Peer Node Navigation 127 Attribute Navigation 128 Updating an X-DOM 128 Simple Value Updates 128 Updating Child Nodes and Attributes 129 Updating Through the Parent 130 Working with Values 133 Setting Values 133 Getting Values 133 Values and Mixed Content Nodes 135 Automatic XText Concatenation 136 viii | Contents
Description: