www.it-ebooks.info LINQ Quickly A practical guide to programming Language Integrated Query with C# N Satheesh Kumar BIRMINGHAM - MUMBAI www.it-ebooks.info LINQ Quickly Copyright © 2007 Packt Publishing All rights reserved. No part of this book may be reproduced, stored in a retrieval system, or transmitted in any form or by any means, without the prior written permission of the publisher, except in the case of brief quotations embedded in critical articles or reviews. Every effort has been made in the preparation of this book to ensure the accuracy of the information presented. However, the information contained in this book is sold without warranty, either express or implied. Neither the author, Packt Publishing, nor its dealers or distributors will be held liable for any damages caused or alleged to be caused directly or indirectly by this book. Packt Publishing has endeavored to provide trademark information about all the companies and products mentioned in this book by the appropriate use of capitals. However, Packt Publishing cannot guarantee the accuracy of this information. First published: November 2007 Production Reference: 1161107 Published by Packt Publishing Ltd. 32 Lincoln Road Olton Birmingham, B27 6PA, UK. ISBN 978-1-847192-54-7 www.packtpub.com Cover Image by Vinayak Chittar ([email protected]) www.it-ebooks.info Credits Author Project Manager N Satheesh Kumar Abhijeet Deobhakta Reviewer Project Coordinator Granville Barnett Patricia Weir Senior Acquisition Editor Indexer Douglas Paterson Hemangini Bari Development Editor Proofreader Nikhil Bangera Cathy Cumberlidge Technical Editor Production Coordinators Sarvesh Shanbhag Aparna Bhagat Shantanu Zagade Editorial Manager Dipali Chittar Cover Designer Aparna Bhagat www.it-ebooks.info About the Author N Satheesh Kumar has a Bachelor's Degree in Computer Science Engineering and has around eleven years of experience in software development lifecycle, project and program management. He started his career developing software applications using Borland software products in a company based in India, and then moved to the United Arab Emirates where he continued developing custom application software using Borland Delphi, and customizing Great Plain Dynamics (now known as Microsoft Dynamics) for an automobile company. He moved back to India and spent three years in design and developing application software using Microsoft products for a top multi national company, and then spent a couple of years in project management and program management activities. Now he works as a Technical Architect for a top retail company based in the United States. He works with the latest Microsoft technologies and has published many articles on LINQ and other features of .NET. www.it-ebooks.info About the Reviewer Granville Barnett's interest in programming has spanned many languages; his talents have been applied most notably at Microsoft. Granville has worked with LINQ since the LINQ May 2006 CTP and has since then advised some of the biggest companies in the world on the successful application of LINQ. Granville has a very active interest in data structures and algorithms, and compiler theory, design and implementation. He would like to thank all at Microsoft, in particular the members of the UK Application Development Consulting team. As an author, Granville has written several magazine and web articles on LINQ. He can be reached through his website at http://gbarnett.org. www.it-ebooks.info www.it-ebooks.info Table of Contents Preface 1 Chapter 1: Overview 5 LINQ Architecture 5 Integration with SQL 7 Integration with XML 7 Support for C# 3.0 Language Features 8 Anonymous Types 9 Object Initializers 11 Collection Initializers 12 Partial Methods 13 Implicitly Typed Local Variables 14 Extensions 15 Expressions 16 Lambda Expressions 16 Query Expressions 18 Expression Trees 22 Summary 24 Chapter 2: LINQ to Objects 25 Array of Integers 25 Collection of Objects 27 Reading from Strings 29 Reading from Text Files 30 Summary 32 Chapter 3: LINQ to XML 33 Features 33 Classes and Hierarchy 34 XElement Class 36 XAttribute Class 36 XDocument Class 36 Other Classes 36 www.it-ebooks.info Table of Contents LINQ to XML with Other XML Technologies 38 LINQ with XmlReader 40 LINQ with XSLT 41 LINQ with MSXML 41 Functional Construction 41 XML Names 44 Loading and Traversing XML 45 Loading XML 46 Traversing XML 46 Data Manipulation 50 Inserting or Adding Elements to XML 50 Inserting or Adding XML Attributes 54 Deleting XML 55 Updating XML 56 Deleting XML Attributes 56 Updating XML Attributes 57 Outputting and Streaming XML 57 Streaming XML 58 Querying XML 59 Query Operators 59 Queries 60 Ancestors and Descendants 63 XML Transformation 64 Dictionaries 65 Convert Dictionary to XML 65 Create Dictionary from XML 66 Writing XML as Text Files and CSV Files 67 Reading from CSV Files 69 LINQ to XML Events 71 XML Literals and Embedded Expressions in Visual Basic 73 Summary 75 Chapter 4: LINQ to SQL 77 Working with Databases Using DataContext 77 Entity Classes 78 Attributes 81 Database Attribute 81 Table Attribute 82 Column Attribute 82 Association Attribute (Foreign Keys) 84 Relationships 85 Function Attribute 87 Parameter Attribute 88 Inheritance Mapping Attribute 88 [ ii ] www.it-ebooks.info Table of Contents Creating and Deleting Databases 89 DataContext Methods 90 Data Manipulation 93 LINQ to SQL Queries 96 Identifying Objects 99 Queries with Multiple Entities 100 Remote Queries and Local Queries 100 Deferred Loading 101 Immediate Loading 103 Projections 105 Constructing XML 106 Joins 107 Raw SQL Query 109 Query Result 109 Stored Procedures 110 User-Defined Functions 116 Class Generator Tool 117 Transactions 121 Handling Concurrency Conflicts 122 Object Relational Designer (O/R Designer) 123 Summary 140 Chapter 5: LINQ over DataSet 141 Loading Data into DataSets 142 Querying Datasets 144 Sequence Operator 146 Querying Typed DataSets 147 DataSet Query Operators 148 CopyToDataTable 149 LoadDataRow 149 Intersect 150 Union 150 Except 151 Field<T> 151 SetField<T> 152 Projection 152 Join 153 SequenceEqual 154 Skip 154 Distinct 154 Summary 154 [ iii ] www.it-ebooks.info