LINQ
UNLEASHED
800 East 96th Street, Indianapolis, Indiana 46240 USA
Paul Kimmel
for C
#
LINQ Unleashed for C#
Copyright © 2009 by Pearson Education, Inc.
All rights reserved. No part of this book shall be reproduced, stored in a retrieval
system, or transmitted by any means, electronic, mechanical, photocopying, recording,
or otherwise, without written permission from the publisher. No patent liability is
assumed with respect to the use of the information contained herein. Although every
precaution has been taken in the preparation of this book, the publisher and author
assume no responsibility for errors or omissions. Nor is any liability assumed for
damages resulting from the use of the information contained herein.
ISBN-13: 978-0-672-32983-8
ISBN-10: 0-672-32983-2
Library of Congress Cataloging-in-Publication Data
Kimmel, Paul.
LINQ unleashed for C# / Paul Kimmel. — 1st ed.
p. cm.
ISBN 978-0-672-32983-8
1. C# (Computer program language) 2. Microsoft LINQ. I. Title.
QA76.73.C154K5635 2009
005.13’3—dc22
2008030703
Printed in the United States of America
First Printing August 2008
Trademarks
All terms mentioned in this book that are known to be trademarks or service marks
have been appropriately capitalized. Sams Publishing cannot attest to the accuracy of
this information. Use of a term in this book should not be regarded as affecting the
validity of any trademark or service mark.
Warning and Disclaimer
Every effort has been made to make this book as complete and as accurate as possi-
ble, but no warranty or fitness is implied. The information provided is on an “as is”
basis. The authors and the publisher shall have neither liability nor responsibility to any
person or entity with respect to any loss or damages arising from the information
contained in this book.
Bulk Sales
Sams Publishing offers excellent discounts on this book when ordered in quantity for
bulk purchases or special sales. For more information, please contact
U.S. Corporate and Government Sales
1-800-382-3419
corpsales@pearsontechgroup.com
For sales outside of the U.S., please contact
International Sales
international@pearson.com
Editor-in-Chief
Karen Gettman
Executive Editor
Neil Rowe
Development Editor
Mark Renfrow
Managing Editor
Kristy Hart
Project Editor
Betsy Harris
Copy Editor
Karen Annett
Indexers
Lisa Stumpf
Publishing Works
Proofreader
Linda Seifer
Technical Editor
Joe Kunk
Publishing
Coordinator
Cindy Teeters
Cover Designer
Gary Adair
Compositor
Jake McFarland
Contents at a Glance
Introduction ...................................................................................1
Part I Getting Ready for LINQ
1 Programming with Anonymous Types
.................................................5
2 Using Compound Type Initialization
.................................................29
3 Defining Extension and Partial Methods
............................................61
4 yield return: Using .NET’s State Machine Generator
.............................85
5 Understanding Lambda Expressions and Closures
................................97
6 Using Standard Query Operators
.....................................................121
Part II LINQ for Objects
7 Sorting and Grouping Queries
........................................................137
8 Using Aggregate Operations
...........................................................151
9 Performing Set Operations
.............................................................167
10 Mastering Select and SelectMany
.....................................................185
11 Joining Query Results
....................................................................211
12 Querying Outlook and Active Directory
...........................................239
Part III LINQ for Data
13 Querying Relational Data with LINQ
...............................................265
14 Creating Better Entities and Mapping Inheritance and Aggregation
.......289
15 Joining Database Tables with LINQ Queries
......................................309
16 Updating Anonymous Relational Data
.............................................349
17 Introducing ADO.NET 3.0 and the Entity Framework
.........................383
Part IV LINQ for XML
18 Extracting Data from XML
.............................................................415
19 Comparing LINQ to XML with Other XML Technologies
....................437
20 Constructing XML from Non-XML Data
...........................................453
21 Emitting XML with the XmlWriter
..................................................463
22 Combining XML with Other Data Models
........................................469
23 LINQ to XSD Supports Typed XML Programming
..............................485
Index .........................................................................................499
Table of Contents
Introduction 1
Conventions Used in This Book
.........................................................2
Part I Getting Ready for LINQ
1 Programming with Anonymous Types 5
Understanding Anonymous Types
......................................................6
Programming with Anonymous Types
.................................................7
Defining Simple Anonymous Types
.............................................7
Using Array Initializer Syntax
.....................................................7
Creating Composite Anonymous Types
.......................................9
Using Anonymous Type Indexes in For Statements
......................12
Anonymous Types and Using Statements
...................................14
Returning Anonymous Types from Functions
..............................17
Databinding Anonymous Types
........................................................18
Testing Anonymous Type Equality
....................................................23
Using Anonymous Types with LINQ Queries
......................................24
Introducing Generic Anonymous Methods
.........................................25
Using Anonymous Generic Methods
..........................................26
Implementing Nested Recursion
...............................................27
Summary
......................................................................................28
2 Using Compound Type Initialization 29
Initializing Objects with Named Types
...............................................30
Implementing Classes for Compound Initialization Through
Named Types
.......................................................................32
Understanding Auto-Implemented Properties
..............................34
Initializing Anonymous Types
..........................................................34
Initializing Collections
....................................................................36
Finishing the Hypergraph
........................................................39
Implementing the Hypergraph Controls Using the Observer
Pattern
...............................................................................47
Using Conversion Operators
............................................................51
ToArray
................................................................................51
OfType
.................................................................................54
Cast
.....................................................................................54
AsEnumerable
........................................................................55