linq in action

572 597 0
linq  in  action

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

Thông tin tài liệu

www.it-ebooks.info LINQ in Action www.it-ebooks.info www.it-ebooks.info LINQ in Action FABRICE MARGUERIE STEVE EICHERT JIM WOOLEY MANNING Greenwich (74° w. long.) www.it-ebooks.info For online information and ordering of this and other Manning books, please visit www.manning.com. The publisher offers discounts on this book when ordered in quantity. For more information, please contact: Special Sales Department Manning Publications Co. Sound View Court 3B fax: (609) 877-8256 Greenwich, CT 06830 email: orders@manning.com ©2008 by Manning Publications Co. All rights reserved. No part of this publication may be reproduced, stored in a retrieval system, or transmitted, in any form or by means electronic, mechanical, photocopying, or otherwise, without prior written permission of the publisher. Many of the designations used by manufacturers and sellers to distinguish their products are claimed as trademarks. Where those designations appear in the book, and Manning Publications was aware of a trademark claim, the designations have been printed in initial caps or all caps. Recognizing the importance of preserving what has been written, it is Manning’s policy to have the books we publish printed on acid-free paper, and we exert our best efforts to that end. Recognizing also our responsibility to conserve the resources of our planet, Manning books are printed on paper that is at least 15% recycled and processed without the use of elemental chlorine. Manning Publications Co. Copyeditor: Benjamin Berg Sound View Court 3B Typesetter: Gordan Salinovic Greenwich, CT 06830 Cover designer: Leslie Haimes ISBN 1-933988-16-9 Printed in the United States of America 12345678910–MAL –131211100908 www.it-ebooks.info v brief contents PART 1GETTING STARTED 1 1 ■ Introducing LINQ 3 2 ■ C# and VB.NET language enhancements 44 3 ■ LINQ building blocks 82 PART 2QUERYING OBJECTS IN MEMORY 113 4 ■ Getting familiar with LINQ to Objects 115 5 ■ Beyond basic in-memory queries 160 PART 3QUERYING RELATIONAL DATA 203 6 ■ Getting started with LINQ to SQL 205 7 ■ Peeking under the covers of LINQ to SQL 237 8 ■ Advanced LINQ to SQL features 267 PART 4MANIPULATING XML 311 9 ■ Introducing LINQ to XML 313 10 ■ Query and transform XML with LINQ to XML 350 11 ■ Common LINQ to XML scenarios 385 www.it-ebooks.info vi BRIEF CONTENTS PART 5LINQING IT ALL TOGETHER 435 12 ■ Extending LINQ 437 13 ■ LINQ in every layer 482 www.it-ebooks.info vii contents foreword xv preface xvii acknowledgments xix about this book xxii PART 1GETTING STARTED 1 1 Introducing LINQ 3 1.1 What is LINQ? 4 Overview 5 ■ LINQ as a toolset 6 ■ LINQ as language extensions 7 1.2 Why do we need LINQ? 9 Common problems 10 ■ Addressing a paradigm mismatch 12 LINQ to the rescue 18 1.3 Design goals and origins of LINQ 19 The goals of the LINQ project 20 ■ A bit of history 21 1.4 First steps with LINQ to Objects: Querying collections in memory 23 What you need to get started 23 ■ Hello LINQ to Objects 25 www.it-ebooks.info viii CONTENTS 1.5 First steps with LINQ to XML: Querying XML documents 29 Why we need LINQ to XML 30 ■ Hello LINQ to XML 32 1.6 First steps with LINQ to SQL: Querying relational databases 37 Overview of LINQ to SQL’s features 37 ■ Hello LINQ to SQL 38 ■ A closer look at LINQ to SQL 42 1.7 Summary 42 2 C# and VB.NET language enhancements 44 2.1 Discovering the new language enhancements 45 Generating a list of running processes 46 ■ Grouping results into a class 47 2.2 Implicitly typed local variables 49 Syntax 49 ■ Improving our example using implicitly typed local variables 50 2.3 Object and collection initializers 52 The need for object initializers 52 ■ Collection initializers 53 Improving our example using an object initializer 54 2.4 Lambda expressions 55 A refresher on delegates 56 ■ Anonymous methods 58 ■ Introducing lambda expressions 58 2.5 Extension methods 64 Creating a sample extension method 64 ■ More examples using LINQ’s standard query operators 68 Extension methods in action in our example 70 Warnings 71 2.6 Anonymous types 73 Using anonymous types to group data into an object 74 Types without names, but types nonetheless 74 Improving our example using anonymous types 76 ■ Limitations 76 2.7 Summary 79 www.it-ebooks.info CONTENTS ix 3 LINQ building blocks 82 3.1 How LINQ extends .NET 83 Refresher on the language extensions 83 ■ The key elements of the LINQ foundation 85 3.2 Introducing sequences 85 IEnumerable<T> 86 ■ Refresher on iterators 87 Deferred query execution 89 3.3 Introducing query operators 93 What makes a query operator? 93 ■ The standard query operators 96 3.4 Introducing query expressions 97 What is a query expression? 98 ■ Writing query expressions 98 ■ How the standard query operators relate to query expressions 100 ■ Limitations 102 3.5 Introducing expression trees 104 Return of the lambda expressions 105 ■ What are expression trees? 105 ■ IQueryable, deferred query execution redux 108 3.6 LINQ DLLs and namespaces 109 3.7 Summary 111 PART 2QUERYING OBJECTS IN MEMORY 113 4 Getting familiar with LINQ to Objects 115 4.1 Introducing our running example 116 Goals 116 ■ Features 117 ■ The business entities 117 Database schema 118 ■ Sample data 118 4.2 Using LINQ with in-memory collections 121 What can we query? 121 ■ Supported operations 126 4.3 Using LINQ with ASP.NET and Windows Forms 126 Data binding for web applications 127 ■ Data binding for Windows Forms applications 133 www.it-ebooks.info [...]... Sample uses of LINQ to SQL in LinqBooks 495 ■ 13.3 Use of LINQ to XML 502 Importing data from Amazon 13.4 13.5 13.6 502 Generating RSS feeds 504 Use of LINQ to DataSet 505 Using LINQ to Objects 509 Extensibility 509 Custom query operators 509 provider 510 13.7 ■ ■ Creating and using a custom LINQ A look into the future 511 Custom LINQ flavors 511 LINQ to XSD, the typed LINQ to XML 513 PLINQ: LINQ meets parallel... provided in both C# and VB.NET at the same time in the book, but they’re all available in both languages in the companion source code Conventions When we write LINQ, ” we’re referring to the LINQ technology or the complete LINQ framework When we write LINQ toolset,” we mean the set of tools LINQ offers: LINQ to Objects, LINQ to XML, LINQ to SQL, and the others We’ll explicitly use LINQ to Objects, LINQ. .. components In this book, we’ll detail the use of three major flavors of LINQ, or LINQ providers LINQ to Objects, LINQ to SQL, and LINQ to XML, respectively in parts 2, 3, and 4 These three LINQ providers form a family of tools that can be used separately for particular needs or combined for powerful solutions We will focus on LINQ to Objects, LINQ to SQL, and LINQ to XML in this book, but LINQ is open... projects while being convinced that LINQ is a joy to work with The intent of this first chapter is to give you an overview of LINQ and to help you identify the reasons to use it We will start by providing an overview of LINQ and the LINQ toolset, which includes LINQ to Objects, LINQ to XML, and LINQ to SQL We will then review some background information to clearly understand why we need LINQ and where... ■ 5.4 Summary 200 PART 3 QUERYING RELATIONAL DATA 203 6 Getting started with LINQ to SQL 205 6.1 Jump into LINQ to SQL Setting up the object mapping DataContext 212 6.2 6.3 207 209 Reading data with LINQ to SQL Refining our queries 217 ■ Setting up the 212 Filtering 217 Sorting and grouping Aggregation 221 Joining 222 ■ ■ www.it-ebooks.info 219 CONTENTS 6.4 6.5 Working with object trees 226 When... uses of LINQ If you want to understand where LINQ comes from, before you dive in, you may be interested in reading the bit of history we propose in chapter 1 www.it-ebooks.info xxiv ABOUT THIS BOOK Part 2 is dedicated to LINQ to Objects and querying in- memory collections This part also contains information about common LINQ use cases and best practices that’ll be useful when working with any LINQ flavor... already offers more LINQ providers than just LINQ to Objects, LINQ to SQL, and LINQ to XML Two of them are LINQ to DataSet and LINQ to Entities (to work www.it-ebooks.info What is LINQ? Figure 1.1 7 LINQ building blocks, LINQ providers, and data sources that can be queried using LINQ with the new ADO.NET Entity Framework) We will present these tools in the second and third parts of this book For now, let’s... meets parallel computing 513 LINQ to Entities, a LINQ interface for the ADO.NET Entity Framework 514 ■ ■ 13.8 bonus chapter: Summary 515 appendix: The standard query operators 517 resources 523 index 527 Working with LINQ and DataSets available online only from www.manning.com/LINQinAction www.it-ebooks.info foreword It’s difficult for me to write this foreword, not because the road to LINQ was long and... represent the LINQ building blocks and toolset in a diagram The LINQ providers presented in figure 1.1 are not standalone tools They can be used directly in your programming languages This is possible because the LINQ framework comes as a set of language extensions This is the second aspect of LINQ, which is detailed in the next section 1.1.3 LINQ as language extensions LINQ allows you to access information... key constituent of LINQ queries Resources provides pointers to resources that will help you to learn more about LINQ, such as Microsoft’s official web sites, articles, weblogs or forums An online bonus chapter available as a download at http://www.manning.com/ LINQinAction and at http://LinqInAction.net introduces LINQ to DataSet It demonstrates how LINQ can be used to query DataSets and DataTables It’s . 509 ■ Creating and using a custom LINQ provider 510 13.7 A look into the future 511 Custom LINQ flavors 511 ■ LINQ to XSD, the typed LINQ to XML 513 ■ PLINQ: LINQ meets parallel computing 513 LINQ. Introducing LINQ 3 1.1 What is LINQ? 4 Overview 5 ■ LINQ as a toolset 6 ■ LINQ as language extensions 7 1.2 Why do we need LINQ? 9 Common problems 10 ■ Addressing a paradigm mismatch 12 LINQ. www.it-ebooks.info LINQ in Action www.it-ebooks.info www.it-ebooks.info LINQ in Action FABRICE MARGUERIE STEVE EICHERT JIM WOOLEY MANNING Greenwich (74° w. long.) www.it-ebooks.info For online information

Ngày đăng: 05/05/2014, 14:55

Từ khóa liên quan

Mục lục

  • LINQ in Action

    • brief contents

    • contents

    • foreword

    • preface

    • acknowledgments

    • about this book

    • Who should read this book

    • How the book is organized

    • Tools used

    • Source code

    • Conventions

    • Typographical conventions

    • Author Online

    • About the authors

    • About the title

    • About the cover illustration

    • Getting started

      • Introducing LINQ

        • 1.1 What is LINQ?

          • 1.1.1 Overview

          • 1.1.2 LINQ as a toolset

          • 1.1.3 LINQ as language extensions

          • 1.2 Why do we need LINQ?

            • 1.2.1 Common problems

Tài liệu cùng người dùng

Tài liệu liên quan