LINQ in Action LINQ in Action FABRICE MARGUERIE STEVE EICHERT JIM WOOLEY MANNING Greenwich (74° w. long.) For online information and ordering of this and other Manning books, please visit www.manning.com. The publisher offers discounts on this book when ordered in quantity. For more information, please contact: Special Sales Department Manning Publications Co. Sound View Court 3B fax: (609) 877-8256 Greenwich, CT 06830 email: [email protected] ©2008 by Manning Publications Co. All rights reserved. No part of this publication may be reproduced, stored in a retrieval system, or transmitted, in any form or by means electronic, mechanical, photocopying, or otherwise, without prior written permission of the publisher. Many of the designations used by manufacturers and sellers to distinguish their products are claimed as trademarks. Where those designations appear in the book, and Manning Publications was aware of a trademark claim, the designations have been printed in initial caps or all caps. Recognizing the importance of preserving what has been written, it is Manning’s policy to have the books we publish printed on acid-free paper, and we exert our best efforts to that end. Recognizing also our responsibility to conserve the resources of our planet, Manning books are printed on paper that is at least 15% recycled and processed without the use of elemental chlorine. Manning Publications Co. Copyeditor: Benjamin Berg Sound View Court 3B Typesetter: Gordan Salinovic Greenwich, CT 06830 Cover designer: Leslie Haimes ISBN 1-933988-16-9 Printed in the United States of America 1 2 3 4 5 6 7 8 9 10 – MAL – 13 12 11 10 09 08 brief contents PART 1 GETTING STARTED....................................................... 1 1 ■ Introducing LINQ 3 2 ■ C# and VB.NET language enhancements 44 3 ■ LINQ building blocks 82 PART 2 QUERYING OBJECTS IN MEMORY ............................... 113 4 ■ Getting familiar with LINQ to Objects 115 5 ■ Beyond basic in-memory queries 160 PART 3 QUERYING RELATIONAL DATA................................... 203 6 ■ Getting started with LINQ to SQL 205 7 ■ Peeking under the covers of LINQ to SQL 237 8 ■ Advanced LINQ to SQL features 267 PART 4 MANIPULATING XML............................................... 311 9 ■ Introducing LINQ to XML 313 10 ■ Query and transform XML with LINQ to XML 350 11 ■ Common LINQ to XML scenarios 385 v vi BRIEF CONTENTS PART 5 LINQING IT ALL TOGETHER..................................... 435 12 ■ Extending LINQ 437 13 ■ LINQ in every layer 482 contents foreword xv preface xvii acknowledgments xix about this book xxii PART 1 GETTING STARTED......................................... 1 1 Introducing LINQ 3 1.1 What is LINQ? 4 Overview 5 LINQ as a toolset 6 LINQ as language ■ ■ extensions 7 1.2 Why do we need LINQ? 9 Common problems 10 Addressing a paradigm mismatch 12 ■ LINQ to the rescue 18 1.3 Design goals and origins of LINQ 19 The goals of the LINQ project 20 A bit of history 21 ■ 1.4 First steps with LINQ to Objects: Querying collections in memory 23 What you need to get started 23 Hello LINQ to Objects 25 ■ vii viii CONTENTS 1.5 First steps with LINQ to XML: Querying XML documents 29 Why we need LINQ to XML 30 Hello LINQ to XML 32 ■ 1.6 First steps with LINQ to SQL: Querying relational databases 37 Overview of LINQ to SQL’s features 37 Hello LINQ to ■ SQL 38 A closer look at LINQ to SQL 42 ■ 1.7 Summary 42 2 C# and VB.NET language enhancements 44 2.1 Discovering the new language enhancements 45 Generating a list of running processes 46 Grouping results ■ into a class 47 2.2 Implicitly typed local variables 49 Syntax 49 Improving our example using implicitly ■ typed local variables 50 2.3 Object and collection initializers 52 The need for object initializers 52 Collection initializers 53 ■ Improving our example using an object initializer 54 2.4 Lambda expressions 55 A refresher on delegates 56 Anonymous ■ methods 58 Introducing lambda expressions 58 ■ 2.5 Extension methods 64 Creating a sample extension method 64 More ■ examples using LINQ’s standard query operators 68 Extension methods in action in our example 70 Warnings 71 2.6 Anonymous types 73 Using anonymous types to group data into an object 74 Types without names, but types nonetheless 74 Improving our example using anonymous types 76 Limitations 76 ■ 2.7 Summary 79 CONTENTS ix 3 LINQ building blocks 82 3.1 How LINQ extends .NET 83 Refresher on the language extensions 83 The key elements ■ of the LINQ foundation 85 3.2 Introducing sequences 85 IEnumerable<T> 86 Refresher on iterators 87 ■ Deferred query execution 89 3.3 Introducing query operators 93 What makes a query operator? 93 The standard query ■ operators 96 3.4 Introducing query expressions 97 What is a query expression? 98 Writing query ■ expressions 98 How the standard query operators relate ■ to query expressions 100 Limitations 102 ■ 3.5 Introducing expression trees 104 Return of the lambda expressions 105 What are ■ expression trees? 105 IQueryable, deferred query ■ execution redux 108 3.6 LINQ DLLs and namespaces 109 3.7 Summary 111 PART 2 QUERYING OBJECTS IN MEMORY................. 113 4 Getting familiar with LINQ to Objects 115 4.1 Introducing our running example 116 Goals 116 Features 117 The business entities 117 ■ ■ Database schema 118 Sample data 118 ■ 4.2 Using LINQ with in-memory collections 121 What can we query? 121 Supported operations 126 ■ 4.3 Using LINQ with ASP.NET and Windows Forms 126 Data binding for web applications 127 Data binding ■ for Windows Forms applications 133
Description: