
ptg
Charlie Calvert
Dinesh Kulkarni
Essential LINQ
Upper Saddle River, NJ • Boston • Indianapolis • San Francisco
New York • Toronto • Montreal • London • Munich • Paris • Madrid
Capetown • Sydney • Tokyo • Singapore • Mexico City
From the Library of Kerri Ross

ptg
Many of the designations used by manufacturers and sellers to distinguish their products are
claimed as trademarks. Where those designations appear in this book, and the publisher was
aware of a trademark claim, the designations have been printed with initial capital letters or
in all capitals.
The .NET logo is either a registered trademark or trademark of Microsoft Corporation in the
United States and/or other countries and is used under license from Microsoft.
The authors and publisher have taken care in the preparation of this book, but make no
expressed or implied warranty of any kind and assume no responsibility for errors or
omissions. No liability is assumed for incidental or consequential damages in connection
with or arising out of the use of the information or programs contained herein.
The publisher offers excellent discounts on this book when ordered in quantity for bulk
purchases or special sales, which may include electronic versions and/or custom covers and
content particular to your business, training goals, marketing focus, and branding interests.
For more information, please contact:
U.S. Corporate and Government Sales
(800) 382-3419
corpsales@pearsontechgroup.com
For sales outside the United States please contact:
International Sales
international@pearson.com
Visit us on the Web: www.informit.com/msdotnetseries
Library of Congress Cataloging-in-Publication Data:
Calvert, Charles
Essential LINQ / Charlie Calvert, Dinesh Kulkarni. — 1st ed.
p. cm.
ISBN 0-321-56416-2 (pbk. : alk. paper) 1. Microsoft LINQ. 2. C# (Computer program
language) 3. Query languages (Computer science) 4. Microsoft .NET Framework.
I. Kulkarni, Dinesh, 1968- II. Title.
QA76.7.C35 2009
006.7’882—dc22
2008052508
Copyright © 2009 Pearson Education, Inc.
All rights reserved. Printed in the United States of America. This publication is protected by
copyright, and permission must be obtained from the publisher prior to any prohibited
reproduction, storage in a retrieval system, or transmission in any form or by any means,
electronic, mechanical, photocopying, recording, or likewise. For information regarding
permissions, write to:
Pearson Education, Inc
Rights and Contracts Department
501 Boylston Street, Suite 900
Boston, MA 02116
Fax (617) 671 3447
ISBN-13: 978-0-321-56416-0
ISBN-10: 0-321-56416-2
Text printed in the United States on recycled paper at RR Donnelley in Crawfordsville,
Indiana.
First printing March 2009
From the Library of Kerri Ross

ptg
This book is dedicated to my wife Margie. When authors dedicate their
books to their wife or parents, I perhaps unfairly suspect them of a fail-
ure of imagination or paucity of emotional range. I would have no prob-
lem coming up with other worthies to whom I could dedicate this book.
My good friends, such as David Intersimone, Lino Tadros, John Kaster,
and Steve Teixeira, have been a joy to me for many years. I’m blessed
with a wonderful father, and I would be happy to dedicate another book
to him. My siblings, nieces, nephews, and other relatives are deserving of
a dedication. So are my godparents, George and MaryAnn Kephart. I’ve
recently been reintroduced to the Pacific Northwest, and I undoubtedly
could never have written a word of this text were it not for the regular
support I get from the mountains, forests, and waterways of this won-
derful land. My many spiritual mentors—Christian, Buddhist, and
Hindu—have been the very air I breathe for many years, and I could do
nothing without them. I also lucked into—through no skill of my own,
and despite all my best efforts to deny myself the privilege—a wonderful
wife to whom I can never sufficiently offer enough thanks. Margie, I
once again find myself dedicating a book to you, wishing only that this
simple dedication were worthy of even some small portion of all that you
have done for me.
—Charlie Calvert
Dedicated to the Lord for all he has given and continues to give.
—Dinesh Kulkarni
From the Library of Kerri Ross

ptg
This page intentionally left blank
From the Library of Kerri Ross

ptg
ix
Contents at a Glance
Foreword xxi
Acknowledgments xxv
About the Authors xxix
1 Introduction 1
2 Getting Started 13
3 The Essence of LINQ 39
4 C# 3.0 Technical Overview 65
5 Writing Query Expressions 129
6 Query Operators 175
7 A Quick Tour of LINQ to SQL 231
8 Reading Objects with LINQ to SQL 247
9 Modifying Objects with LINQ to SQL 281
10 Using Stored Procedures and Database Functions
with LINQ to SQL 319
11 Customizing Entity Persistence and Adding Business Logic 337
12 LINQ to Entities Overview 349
13 LINQ to XML: Creation 369
From the Library of Kerri Ross