800 East 96th Street, Indianapolis, Indiana 46240 USA
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
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.
Every effort has been made to make this book as complete and as accurate as possible, 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.
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
[email protected]
For sales outside of the U.S., please contact
International Sales
[email protected]
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
14 Creating Better Entities and Mapping Inheritance and Aggregation |
Data affects just about every aspect of our lives. Everything we do is analyzed, scrutinized, and delivered back to us in the form of coupons and other marketing materials. When you write an application, you can be sure that data in one form or another will be part of the solution. As software developers, the ease with which we can store, retrieve, and analyze data is crucial to our ability to develop compelling applications. Add to that the fact that data can come in a number of different shapes and formats, and it quickly comes to light that there is tremendous value in a consistent framework for accessing many types of data.
Several different data access approaches have been developed for Windows developers over the years. ADO and OLEDB and subsequently ADO.NET gave us universal access to relational databases. MSXML and ADO.NET made it possible to inspect and manipulate XML documents. Each of these technologies had their benefits and drawbacks, but one common thread ran through each of them: They failed to deliver data access capabilities in a way that felt natural to developers.
LINQ now makes data access a first-class programming concept in .NET, making it possible for developers to express queries in a way that makes sense to developers. What makes LINQ unique is that it enables programmers to create type-safe data access code complete with Intellisense support and compile time syntax checking.
Paul Kimmel has done an excellent job of presenting LINQ in a concise and complete manner. Not only has he made LINQ approachable, but he has also masterfully explained concepts such as Anonymous Types and Lambda Expressions that help make LINQ a reality. The sample code throughout the book demonstrates the application of the technology in a clear and meaningful way. This is a great “Saturday morning with a pot of coffee” kind of book. I hope you’ll dive in and get as much out of this book as I did.
Darryl Hogan
Architect Evangelist, Microsoft
Paul Kimmel is a four-time Microsoft MVP, the author of over a dozen books on object-oriented programming and UML, including three books on Microsoft .NET, a columnist for codeguru.com, developer.com, informit.com, devsource.com, and devx.com, a cofounder of the Greater Lansing Area .NET Users Group (glugnet.org, East Lansing and Flint), a full-time software developer, and sometimes pilot. Paul still lives and works in the greater Lansing, Michigan, area (and hasn’t given up on the economy). After 15 years of independent consulting, Paul now works for EDS as an application architect.
This book is dedicated to the men and women of the United States armed forces, especially those brave souls serving in conflict zones, away from hearth and kin. To all military police men and women—Lookout Firefly it’s Night-train! Mohawk, 38 out.
Sometimes I read other people’s acknowledgements but a long list of names is boring unless it’s your name. If the casual reader is reading this, then you are helping me thank the myriad of people that make a book like this possible. If you are one of the people listed, then know that your able assistance is greatly appreciated.
Thanks to Neil Rowe, my acquisitions editor; Curt Johnson, marketing manager at Pearson; and Joan Murray, editor, also at Pearson. I wrote slower but finishing a book is thrilling.
Thanks to Joe Kunk for not complaining about how much work technical editing is and actually testing the examples. Joe also does a lot of heavy lifting at Glugnet East Lansing and Flint (our .NET User Groups). Thanks to the rest of the glugnet board for covering for me in my absence: Vivek Joshi, Alireza Namvar, Eric Vogel, Jeff McWherter, Vijay Jagdale, Jason Harris, Aaron Lilywhite, and Rich Hamilton.
Thanks to Bart De Smet from Microsoft for assistance with the Active Directory IQueryable provider—LINQ to AD is cool. Thanks to Daryl Hogan for the foreword.
I’d also like to mention Brian Dawson, Brad Jones, Tyler Durden, Jackson Wayfare, Blizzard for War Craft and Rockstar games for GTA IV, my kids Alex and Noah, Dena and Joe Swanson (for wine and comic relief), Ed Swanson for cigars, and all of the great folks at Pearson behind the scenes that turn my six-fingered manuscript into a shiny new book.
As the reader of this book, you are our most important critic and commentator. We value your opinion and want to know what we’re doing right, what we could do better, what areas you’d like to see us publish in, and any other words of wisdom you’re willing to pass our way.
You can email or write me directly to let me know what you did or didn’t like about this book—as well as what we can do to make our books stronger.
Please note that I cannot help you with technical problems related to the topic of this book, and that due to the high volume of mail I receive, I might not be able to reply to every message.
When you write, please be sure to include this book’s title and author, as well as your name and phone or email address. I will carefully review your comments and share them with the author and editors who worked on the book.
E-mail: [email protected]
Mail: Neil Rowe
Executive Editor
Sams Publishing
800 East 96th Street
Indianapolis, IN 46240 USA
Visit our website and register this book at www.informit.com/register for convenient access to any updates, downloads, or errata that might be available for this book.
3.135.200.14