
ptg
LINQ
TO
O
BJECTS
U
SING
C# 4.0
U
SING AND
E
XTENDING
LINQ
TO
O
BJECTS AND
P
ARALLEL
LINQ (PLINQ)
Troy Magennis
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 Wow! eBook

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 trade-
mark claim, the designations have been printed with initial capital letters or in all capitals.
The author 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: informit.com/aw
Library of Congress Cataloging-in-Publication Data:
Magennis, Troy, 1970-
LINQ to objects using C# 4.0 : using and extending LINQ to objects and parallel LINQ (PLINQ) /
Troy Magennis.
p. cm.
Includes bibliographical references and index.
ISBN 978-0-321-63700-0 (pbk. : alk. paper) 1. Microsoft LINQ. 2. Query languages (Computer sci-
ence) 3. C#
(Computer program language) 4. Microsoft .NET Framework. I. Title.
QA76.73.L228M345 2010
006.7’882—dc22
2009049530
Copyright © 2010 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-63700-0
ISBN-10: 0-321-63700-3
Text printed in the United States on recycled paper at RR Donnelly in Crawfordsville, Indiana.
First printing March 2010
From the Library of Wow! eBook

ptg
To my wife, Janet Doherty, for allowing me to spend those
extra hours tapping away on the keyboard; thank you for
your support and love.
From the Library of Wow! eBook

ptg
This page intentionally left blank
From the Library of Wow! eBook

ptg
vii
C
ONTENTS
Foreword . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . x
Preface . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xii
Acknowledgments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xix
About the Author . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xx
Chapter 1: Introducing LINQ . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1
What Is LINQ? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1
The (Almost) Current LINQ Story . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
LINQ Code Makeover—Before and After Code Examples . . . . . . . . . . . 5
Benefits of LINQ . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15
References . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15
Chapter 2: Introducing LINQ to Objects . . . . . . . . . . . . . . . . . . . . . 17
LINQ Enabling C# 3.0 Language Enhancements . . . . . . . . . . . . . . . . 17
LINQ to Objects Five-Minute Overview . . . . . . . . . . . . . . . . . . . . . . . 30
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 39
References . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 39
Chapter 3: Writing Basic Queries . . . . . . . . . . . . . . . . . . . . . . . . . . 41
Query Syntax Style Options . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 41
How to Filter the Results (Where Clause) . . . . . . . . . . . . . . . . . . . . . . 49
How to Change the Return Type (Select Projection) . . . . . . . . . . . . . . . 54
How to Return Elements When the Result
Is a Sequence (Select Many) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 59
How to Get the Index Position of the Results . . . . . . . . . . . . . . . . . . . 61
How to Remove Duplicate Results . . . . . . . . . . . . . . . . . . . . . . . . . . . 62
How to Sort the Results . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 63
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 73
From the Library of Wow! eBook