LINQ For Dummies - John Paul Mueller - E-Book

LINQ For Dummies E-Book

John Paul Mueller

0,0
20,99 €

-100%
Sammeln Sie Punkte in unserem Gutscheinprogramm und kaufen Sie E-Books und Hörbücher mit bis zu 100% Rabatt.
Mehr erfahren.
Beschreibung

If you’ve asked yourself “Why can’t I develop database and XML queries in a language I already know?”, then Language INtegrated Query, or LINQ, is for you. LINQ For Dummies introduces you to LINQ and the .NET Framework technologies, so you can use LINQ to query any object, any data set, any kind of XML, and SQL Server—no questions asked.

This plain-English guide gives you a thorough overview of LINQ, from understanding the tasks it performs to making LINQ work with both Visual Basic and Visual Studio 2005. It explains the four LINQ providers in the .NET Framework, the easiest ways to go about accessing data, and how to write more efficient applications with less code using LINQ. There’s also clear guidance on combining third-party providers with LINQ to create even more powerful apps. With this single, comprehensive guide, you’ll discover how to:

  • Use one query language with all Microsoft languages
  • Examine .NET language extensions and work with extension methods, partial methods, lambda expressions, and query expressions
  • LINQ to DataSet operators, SQL server operations, XML API, or Active Directory
  • Deal with databases — download and install the Northwind database, generate Northwind entity classes, and create the Northwind XML mapping file
  • Create the partial class example, the partial method example, and the database modification example
  • Use objects with LINQ
  • Query databases in Visual Basic and C#

As an added bonus, you can visit the companion Web site for LINQ examples in C# and Visual Basic. With LINQ For Dummies, you’ll link up with LINQ in no time and see how you can query almost anything!

Note: CD-ROM/DVD and other supplementary materials are not included as part of eBook file.

Sie lesen das E-Book in den Legimi-Apps auf:

Android
iOS
von Legimi
zertifizierten E-Readern

Seitenzahl: 482

Veröffentlichungsjahr: 2009

Bewertungen
0,0
0
0
0
0
0
Mehr Informationen
Mehr Informationen
Legimi prüft nicht, ob Rezensionen von Nutzern stammen, die den betreffenden Titel tatsächlich gekauft oder gelesen/gehört haben. Wir entfernen aber gefälschte Rezensionen.



LINQ For Dummies®

Table of Contents

Introduction

About This Book

Conventions Used in This Book

What You Should Read

What You Don’t Have to Read

Foolish Assumptions

How This Book Is Organized

Part I: An Overview of LINQ

Part II: Using Standard LINQ to Technologies

Part III: Extending LINQ to New Horizons

Part IV: The Part of Tens

The accompanying Web site

Icons Used in This Book

Where to Go from Here

Part I: An Overview of LINQ

Chapter 1: Getting to Know LINQ

Considering LINQ

Understanding the task that LINQ performs

Contemplating why you need LINQ

Defining the LINQ design goals

Understanding the LINQ requirements

Using LINQ in the Real World

Understanding Declarative Programming

An Overview of the LINQ Namespaces

System.Linq namespace

System.Linq.Expressions namespace

System.Data.Linq namespace

System.Data.Linq.Mapping namespace

System.Data.SqlClient namespace

System.Data.SqlClient.Implementation namespace

System.Xml.Linq namespace

Chapter 2: Creating Simple LINQ Queries

Defining the LINQ Keywords

Working with from

Working with where

Working with orderby

Working with join

Working with let

Creating a Simple from Query

Understanding the simple query application code

Using the debugger to see the simple query in action

Filtering a Query

Grouping Queries

Creating Queries from Multiple Sources

Understanding the multiple-source application code

Working with the alternative multiple-source example

Using calculated values in multiple-source queries

Working with the Standard Query Operators

Chapter 3: Considering the .NET Language Ex tensions

An Overview of the Language Extensions

Working with Lambda Expressions

Understanding the Role of the IEnumerable and IEnumerable<T> Interfaces

Understanding Object Initializers

Understanding Collection Initializers

Working with Extension Methods

Working with Partial Methods

An Overview of Expression Trees

Working with Query Expressions

Chapter 4: Working with LINQ in Visual Basic .NET

Understanding the Visual Basic Differences

Creating the Simple Visual Basic Example

Working with From

Working with Where

Working with Order By

Working with Join

Working with Let

Using the Additional Visual Basic Keywords

Working with Aggregate

Working with Distinct

Working with Skip

Working with Take

Working with Lambda Functions in Visual Basic

Chapter 5: Working with LINQ in Visual Studio 2005

Obtaining LINQ Support for Visual Studio 2005

Installing LINQ Support

Creating the Simple Visual Studio 2005 Project

Defining the project

Working with from

Working with join

Working with where

Working with orderby

Working with let

Part II: Using Standard LINQ to Technologies

Chapter 6: LINQ to Object

Considering the Use of Objects with LINQ

Creating the Simple Object Query Example

Understanding the Role of Deferred Operators

Creating the Deferred Operator Examples

Working with Concat

Working with AsEnumerable, AsQueryable, Cast, and OfType

Working with OfType and Where

Working with DefaultIfEmpty, Empty, Range, and Repeat

Working with GroupBy and ToLookup

Working with GroupJoin and Join

Working with Skip, SkipWhile, Take, and TakeWhile

Working with Select and SelectMany

Working with Distinct, Except, Intersect, and Union

Working with OrderBy, OrderByDescending, Reverse, ThenBy, and ThenByDescending

Understanding the Role of Nondeferred Operators

Creating the Nondeferred Operator Examples

Working with Aggregate, Average, Count, LongCount, Max, Min, and Sum

Working with ToArray, ToDictionary, ToList, and ToLookup

Working with ElementAt, ElementAtOrDefault, First, FirstOrDefault, Last, LastOrDefault, Single, and SingleOrDefault

Working with SequenceEqual

Working with All, Any, and Contains

Chapter 7: LINQ to DataSet

Considering the Use of DataSets with LINQ

Creating the Simple DataSet Example

Defining the test tables

Outputting the results

Exploring the connection

Understanding the LINQ to DataSet Operators

Creating the Filtered Output Example

Creating a simple display

Modifying the filtered output

Using the CopyToDataTable operator

Working with typed DataSets

Working with Multiple DataSet Tables

Chapter 8: LINQ to SQL Server

Considering the Use of SQL Server with LINQ

Obtaining and Installing the Northwind Database

Downloading the database

Installing the database

Testing the Visual Studio connection

Generating the Northwind entity classes and XML mapping files

Creating the Simple SQL Server Query Example

Overcoming the Visual Studio 2008 connectivity issues

Defining the project

Adding the code

Viewing the debugger output

Using Object Relational Designer

Understanding the LINQ to SQL Server Operators

Chapter 9: LINQ to XML

Considering the Use of XML with LINQ

Working with the LINQ to XML API

Understanding the LINQ to XML Operators

Working with XDeclaration, XElement, and XDocument

Working with XNamespace

Working with XProcessingInstruction

Working with XAttribute

Working with XComment and XText

Working with XCData

Working with XDocumentType

Working with XNodeDocumentOrderComparer

Working with XNodeEqualityComparer

Working with Remove

Working with XName

Creating the Simple XML File Example

Creating the project

Building an XML document

Saving an XML document

Loading an XML document

Part III: Extending LINQ to New Horizons

Chapter 10: Using LINQ with Office 2007

Understanding the Office Document Structure

Understanding LINQ Interaction with Office

Obtaining the Required Library

Creating the Office 2007 Document Example

Creating the project

Understanding the custom properties

Reading document properties

Writing document properties

Chapter 11: Advanced LINQ to SQL Server

Considering SQL Server Issues

Understanding concurrency problems

Overcoming performance issues

Creating the Database Modification Example

Performing an insert

Performing an update

Performing a delete

Using Concurrency Checks and Resolving Errors

Resolving conflicts at the object level

Resolving conflicts at the member level

Chapter 12: LINQ to Active Directory

Working with Specific Active Directory Objects

Defining Active Directory Variables

Obtaining the LINQ to Active Directory Provider

Creating the Simple Active Directory Query Example

Performing the project setup

Defining an Active Directory object class

Creating a root node pointer

Reading user information

Writing user information

Understanding the Limitations of Active Directory Interaction

Some LINQ methods aren’t implemented

Defining the need for LDAP patience

Chapter 13: Other LINQ to Strategies

Understanding the Qualifications for a LINQ to Solution

Accessing COM+ Using LINQ

COM+ accessed as a Web service

COM+ accessed using interop functionality

Creating the Resource Description Format (RDF) Files Example

A quick overview of RDF

A quick overview of SPARQL

Starting the project

Configuring the project

Creating the host application

Creating the application class definition

Reading RDF files

Creating the MySQL Example

Getting the MySQL ADO.NET connector

Compiling the DbLinq.DLL and DbLinq.MySQL.DLL files

Configuring the database

Defining the MySQL project

Developing the query code

Part IV: The Part of Tens

Chapter 14: Ten Ways to Improve LINQ Development

Using LINQ Tools to Simplify Coding

Getting the VLinq add-in application

Creating a VLinq query

Using a VLinq query in an application

Using LINQ to Create Self Documenting Code

Analyzing Code Patterns

Querying a Code Snippet Database

Locating Other Development Resources

Using LINQ to Query Data Formats

Finding Usage Trends

Developing a LINQ Library

Sharing LINQ Queries with Others

Analyzing Compiler and IDE Output

Chapter 15: Ten Ways to Reduce Application Support Costs

Creating Self-Modifying Queries

Using LINQ to Create Reports

Addressing User Search Needs

Creating User-Friendly Mashups

Making Help More Accessible

Organizing and Querying Support Requests and Responses

Developing Fast Searches from Multiple Sources

Helping Users Locate Existing Resources

Performing Support Task Automation

Improving Application and System Management

Chapter 16: Ten LINQ Resources

Starting with the Microsoft Developer Network

Getting Tips from the Microsoft Blogs

Finding Help in Third-Party Web Sites

Finding Help in Third-Party Newsletters and Blogs

Using Other Sources for LINQ to Objects Projects

Using Other Sources for LINQ to SQL Server Projects

Using Other Sources for LINQ to XML Projects

Considering Other LINQ to Sources Projects

Getting Help with Visual Basic Projects

Getting Help with C# Projects

LINQ For Dummies®

by John Paul Mueller

LINQ For Dummies®

Published byWiley Publishing, Inc.111 River St.Hoboken, NJ 07030-5774www.wiley.com

Copyright © 2009 by Wiley Publishing, Inc., Indianapolis, Indiana

Published simultaneously in Canada

No part of this publication may be reproduced, stored in a retrieval system or transmitted in any form or by any means, electronic, mechanical, photocopying, recording, scanning or otherwise, except as permitted under Sections 107 or 108 of the 1976 United States Copyright Act, without either the prior written permission of the Publisher, or authorization through payment of the appropriate per-copy fee to the Copyright Clearance Center, 222 Rosewood Drive, Danvers, MA 01923, (978) 750-8400, fax (978) 646-8600. Requests to the Publisher for permission should be addressed to the Permissions Department, John Wiley & Sons, Inc., 111 River Street, Hoboken, NJ 07030, (201) 748-6011, fax (201) 748-6008, or online at http://www.wiley.com/go/permissions.

Trademarks: Wiley, the Wiley Publishing logo, For Dummies, the Dummies Man logo, A Reference for the Rest of Us!, The Dummies Way, Dummies Daily, The Fun and Easy Way, Dummies.com, Making Everything Easier, and related trade dress are trademarks or registered trademarks of John Wiley & Sons, Inc. and/or its affiliates in the United States and other countries, and may not be used without written permission. All other trademarks are the property of their respective owners. Wiley Publishing, Inc., is not associated with any product or vendor mentioned in this book.

Limit of Liability/Disclaimer of Warranty: The publisher and the author make no representations or warranties with respect to the accuracy or completeness of the contents of this work and specifically disclaim all warranties, including without limitation warranties of fitness for a particular purpose. No warranty may be created or extended by sales or promotional materials. The advice and strategies contained herein may not be suitable for every situation. This work is sold with the understanding that the publisher is not engaged in rendering legal, accounting, or other professional services. If professional assistance is required, the services of a competent professional person should be sought. Neither the publisher nor the author shall be liable for damages arising herefrom. The fact that an organization or Website is referred to in this work as a citation and/or a potential source of further information does not mean that the author or the publisher endorses the information the organization or Website may provide or recommendations it may make. Further, readers should be aware that Internet Websites listed in this work may have changed or disappeared between when this work was written and when it is read.

For general information on our other products and services, please contact our Customer Care Department within the U.S. at 800-762-2974, outside the U.S. at 317-572-3993, or fax 317-572-4002.

For technical support, please visit www.wiley.com/techsupport.

Wiley also publishes its books in a variety of electronic formats. Some content that appears in print may not be available in electronic books.

Library of Congress Control Number: 2008931623

ISBN: 9780470507612

Manufactured in the United States of America

10 9 8 7 6 5 4 3 2 1

About the Author

John Paul Mueller is a freelance author and technical editor. He has writing in his blood, having produced 80 books and over 300 articles to date. The topics range from networking to artificial intelligence and from database management to heads-down programming. Some of his current books include a Windows power optimization book, books on both Windows Server 2008 GUI and Windows Server 2008 Server Core, and a programmer’s guide that discusses the new Office Fluent User Interface (RibbonX). His technical editing skills have helped more than 56 authors refine the content of their manuscripts. John has provided technical editing services to both Data Based Advisor and Coast Compute magazines. He’s also contributed articles to the following magazines: CIO.com, DevSource, InformIT, Informant, DevX, SQL Server Professional, Visual C++ Developer, Hard Core Visual Basic, asp.netPRO, Software Test and Performance, and Visual Basic Developer.

When John isn’t working at the computer, you can find him in his workshop crafting wood projects or making candles. On any given afternoon, you can find him working at a lathe or putting the finishing touches on a bookcase. He also likes making glycerin soap, which comes in handy for gift baskets. You can reach John on the Internet at [email protected]. John is also setting up a Web site and blog at http://www.johnmuellerbooks.com; feel free to look and make suggestions on how he can improve it.

Dedication

This book is dedicated to my friend Osvaldo Téllez Almirall (Chicho). We’ve had many conversations about music and life in general and I’ve always felt that I came away a better person for them.

Author’s Acknowledgments

Thanks to my wife, Rebecca, for working with me to get this book completed. I really don’t know what I would have done without her help in researching and compiling some of the information that appears in this book. She also did a fine job of proofreading my rough draft.

Russ Mullen deserves thanks for his technical edit of this book. He added greatly to the accuracy and depth of the material that you see here. I appreciated the time he devoted to checking my code for accuracy. As I wrote this book, I also spent a good deal of time bouncing ideas off Russ, which is a valuable aid to any author.

Matt Wagner, my agent, deserves credit for helping me get the contract in the first place and taking care of all the details that most authors don’t consider. I always appreciate his assistance. It’s good to know that someone wants to help.

A number of people read all or part of this book to help me refine the approach, test the examples, and generally provide input that every reader wishes they could have. These unpaid volunteers helped in ways too numerous to mention here. I especially appreciate the efforts of Osvaldo Téllez Almirall, who provided me with some interesting bits of information. Bill Salkin gave me some good ideas. Andrew Matthews provided me with significant help and coding examples for the LINQ to RDF provider. In fact, a number of people made suggestions on how to improve my coding technique. I’d like to thank each person who wrote me with an idea by name, but there are simply too many.

Finally, I would like to thank Katie Feltman, Susan Pink, and the rest of the editorial and production staff for their assistance in bringing this book to print. It’s always nice to work with such a great group of professionals.

Publisher’s Acknowledgments

We’re proud of this book; please send us your comments through our online registration form located at www.dummies.com/register/.

Some of the people who helped bring this book to market include the following:

Acquisitions, Editorial, and Media Development

Project Editor: Susan Pink

Acquisitions Editor: Katie Feltman

Copy Editor: Susan Pink

Technical Editor: Ross Mullen

Editorial Manager: Jodi Jensen

Assistant Project Manager: Jennny Swisher

Assistant Producer: Shawn Patrick

Editorial Assistant: Amanda Foxworth

Sr. Editorial Assistant: Cherie Case

Cartoons: Rich Tennant (www.the5thwave.com)

Composition Services

Project Coordinator: Katherine Key

Layout and Graphics: Stacie Brooks, Reuben W. Davis, Melissa K. Jester, Christin Swinford

Proofreader: Linda Seifert, Amanda Steiner

Indexer: Sharon Shock

Publishing and Editorial for Technology Dummies

Richard Swadley, Vice President and Executive Group Publisher

Andy Cummings, Vice President and Publisher

Mary Bednarek, Executive Acquisitions Director

Mary C. Corder, Editorial Director

Publishing for Consumer Dummies

Diane Graves Steele, Vice President and Publisher

Joyce Pepple, Acquisitions Director

Composition Services

Gerry Fahey, Vice President of Production Services

Debbie Stailey, Director of Composition Services

Introduction

Language INtegrated Query (LINQ) sounds like some kind of formless question that depends on how someone speaks. Of course, your queries are integrated with your language! How can you make them otherwise?

LINQ is actually a new feature of .NET Framework that expands your grasp well beyond things Microsoft. In fact, after playing with LINQ for quite some time I thought I’d start to run out of new data sources to query, but it hasn’t happened yet. You can use LINQ to query just about anything (and I only say “just about” because I’m sure there is something LINQ can’t reach).

The thing that should excite you most about LINQ is that it helps you access technologies that you couldn’t ordinarily access or couldn’t do so with ease. For example, you can use LINQ to access MySQL easily, without considering a workaround. You can also access Resource Description Framework (RDF) files, something that would be tough without LINQ. You get all of these additional opportunities to access varied data sources and you’ll write less code, not more, to do it. LINQ is truly one of the most interesting features that Microsoft has ever added to .NET Framework, and you’d miss out on a lot if you didn’t explore the LINQ technology for your own programming needs.

About This Book

LINQ For Dummies is your gateway to a new world — one in which you can ask questions (programmatically) of literally anything. This book starts with the basics. You need to discover the LINQ basics before you can begin pulling information from Active Directory, but LINQ is so small compared to many technologies that the basics require only a few pages. Most of this book is about getting serious work accomplished.

After working with LINQ for a while, I discovered that there are inequities in the LINQ world. For one thing, it seems as if Microsoft is avoiding Visual Basic .NET because their Web site has a decided lack of Visual Basic .NET materials. It was hard to find Visual Basic .NET examples on third-party Web sites too. I hope that this situation will change, but for now you have Chapter 4, which tells you the Visual Basic .NET side of the story, and Chapter 16, which provides you with a host of Visual Basic .NET resources.

The second inequity that LINQ For Dummies addresses is the needs of the Visual Studio 2005 developer. It appears that everyone assumes you have Visual Studio 2008. Although most of this book was written with Visual Studio 2008 in mind, there isn’t any reason you can’t write LINQ applications using Visual Studio 2005, and Chapter 5 provides the information you need. The remaining examples in the book will even work with Visual Studio 2005 with a little tweaking.

You’ll also find a host of example types in LINQ For Dummies. For example, did you know that you can access the content of Office 2007 documents using LINQ? Chapter 10 tells you about all of the interesting things you can do with Office 2007 once you have LINQ at your disposal. The astonishing thing is that you can perform these tasks with little code. Using LINQ, it’s possible to both read and write Office 2007 documents without resorting to odd programming strategies.

LINQ can also make your life as a developer considerably easier. Chapter 14 provides an array of interesting techniques you can use to obtain information about your applications that you might not have even thought available. Only in LINQ For Dummies can you find all of these mind-boggling techniques in one place.

Conventions Used in This Book

I always try to show you the fastest way to accomplish any task. In many cases, this means using a menu command such as File⇒New⇒Project. When working with LINQ, I’ll tell you which dialog box tab to access first, and then which feature to use on that tab.

This book also uses special type to emphasize some information. For example, entries that you need to type appear in bold. All code, Web site URLs, and on-screen messages appear in monofont type. When I define a new word, you’ll see that word in italics.

Because you use multiple applications when you’re working with LINQ, I always point out when to move from one application to the next. When a chapter begins, I introduce the main application for that chapter. All the commands in that chapter are for the main application until I specifically tell you to move to another application. I also tell you when it’s time to move back to the main application.

What You Should Read

Anyone who doesn’t have any LINQ experience should read all of Part I. It’s possible to skip Chapter 1 if you don’t want to know about the array of LINQ providers already on the market or the other interesting things you can do with LINQ, but most people have a number of special needs and Chapter 1 is where you discover how to fulfill them.

Someone who’s been exposed to LINQ but hasn’t worked with it can probably skip Part I and begin with Part II. Make sure you read the chapters in turn. No, the chapters don’t provide interlocked information, forcing you to grind through one chapter at a time, but the LINQ to DataSet provider does build on the LINQ to Objects provider, so you need to know both to create a LINQ to Objects application.

If you’re familiar with the basic LINQ to providers (you have written programs using them) and just want to see the other things that LINQ can do, you’ll want to start with Part III. In some cases, you may have questions about a particular technique and you can refer to Parts I and II when you need to answer such questions. Part III doesn’t provide quite as much hand holding, so you do need to know how LINQ works to use it.

What You Don’t Have to Read

Most of the chapters contain some advanced material that will interest only some readers. When you see one of these specialized topics (such as the requirements for using COM+ in Vista and Windows Server 2008 in Chapter 13), feel free to skip it. Most of this advanced material appears in sidebars and some of it applies only when you use a specific operating system. The sidebar title will always indicate the special nature of the advanced material.

You can also skip any material marked with a Technical Stuff icon. This material is helpful, but you don’t have to know it to work with LINQ. I include this material because I find it helpful in my programming efforts and hope that you will, too.

Foolish Assumptions

You might find it difficult to believe that I’ve assumed anything about you — after all, I haven’t even met you yet! Although most assumptions are indeed foolish, I made these assumptions to provide a starting point for the book.

I’m assuming that you’ve worked with Windows long enough to know how the keyboard and mouse work. You should also know how to use menus and other basic Windows features. It’s also essential to know how to use the Visual Studio IDE and that you know at least one .NET language. This book doesn’t provide any instruction on how to write applications outside the instructions needed to write LINQ applications. If you don’t know what a foreach loop is, you definitely require a different book.

Some portions of the book work with Web pages and others use XML; you need to know at least a little about these technologies to use those sections. You don’t have to be an expert in any of these areas, but more knowledge is better. You must also have a very good knowledge of the programming language you use to work with LINQ.

This is a book for someone who has development experience. I’m assuming that you have a very good knowledge of either Visual Basic .NET or C#. You must also know how to work with any extension technology you want to access. For example, if you want to write a LINQ query to access COM+, you need to know something about COM+ before you can read about it in this book. The same holds true for Active Directory, RDF, MySQL, Office 2007, or any of the other extension technologies described in the book.

How This Book Is Organized

This book contains several parts. Each part demonstrates a particular LINQ concept. In each chapter, I discuss a particular topic and include example programs that you can use to discover more about LINQ on your own. You can find the source code for this book on the Dummies.com Web site at http://www.dummies.com/go/linqfd.

Part I: An Overview of LINQ

Part I looks at LINQ as a technology and introduces you to essential concepts such as the operators used to create a LINQ query. In addition, this part reviews the .NET Framework technologies that make LINQ queries possible. This is also the part of the book that helps you use LINQ with both Visual Basic .NET and Visual Studio 2005. One of the central themes of this part of LINQ For Dummies is helping you discover all the tasks that LINQ can perform.

Part II: Using Standard LINQ to Technologies

.NET Framework comes with four providers. A provider is a link between your application and the data source it requires. Each of the four LINQ providers that come with .NET Framework appear in a separate chapter in Part II. Using just these four providers, you can query any object, any data set, any kind of XML, and SQL Server. These four providers cover a lot of ground. Just knowing them will give you access to a considerable range of data, much of which isn’t even hosted on Windows.

Part III: Extending LINQ to New Horizons

The four basic LINQ providers can perform quite a few tasks, but they have limits. Sometimes it isn’t a matter of being able to access the data, but merely one of accessing the data without a lot of extra programming. Part III shows how you can combine third-party providers with LINQ to create some remarkable applications. Because the applications in this part of the book are unique, you’ll want to skip around a bit and read about the technologies that interest you the most first.

Part IV: The Part of Tens

The final part of the book provides you with some helpful tips and resources you can use to make your LINQ development experience even better. Chapter 14 shows how you can use LINQ to create a better development environment for yourself. Chapter 15 helps you discover ways to use LINQ to make the application environment better for users and support staff, while reducing your workload. Chapter 16 provides ten truly useful resources that will help reduce your development time. If you have the idea by now that LINQ is all about making things easier, you have understood the intent of this book.

The accompanying Web site

This book contains a lot of code, and you might not want to type it. In fact, it’s probably better if you don’t attempt to type this code manually. Fortunately, you can find the source code for this book on the Dummies.com Web site at http://www.dummies.com/go/linqfd. The source code is organized by chapter, and I’ll always tell you about the example files in the text. The best way to work with a chapter is to download all the source code for it at one time.

Icons Used in This Book

As you read this book, you’ll see icons in the margins that indicate material of interest (or not, as the case may be).This section briefly describes each icon in this book.

Tips are nice because they help you save time or perform some task without a lot of extra work. The tips in this book are timesaving techniques or pointers to resources that you should try to get the maximum benefit from LINQ.

I don’t want to sound like an angry parent or some kind of maniac, but you should avoid doing anything marked with a Warning icon. Otherwise, you could find that your program melts down and takes your data with it.

Whenever you see this icon, think advanced tip or technique. You might find these tidbits of useful information just too boring for words, or they could contain the solution you need to get a program running. Skip these bits of information whenever you like.

If you don’t get anything else out of a particular chapter or section, remember the material marked by this icon. This text usually contains an essential process or bit of information that you must know to write LINQ programs successfully.

Where to Go from Here

It’s time to start your LINQ adventure! I recommend that anyone who has only a passing knowledge of LINQ go right to Chapter 1. This chapter contains essential, get-started information that you need for writing your first LINQ program.

If you already have some LINQ experience, move right to Chapter 6. It is important to know about the four LINQ providers that come with .NET Framework. Otherwise, you might find that it’s hard to understand how the third-party LINQ providers described in Part III work. If you desperately need LINQ on your next project, read through Chapter 6 first before you move on to Part III because every LINQ provider available today builds on the LINQ to Objects provider.

Part I

An Overview of LINQ

In this part . . .

Someone once said that the beginning is a good place to start; well, this is the beginning. Chapter 1 introduces you to LINQ — it clears up any questions you might have about what LINQ is and what it can do. Most importantly, it introduces you to a considerably larger view of LINQ than you might have at the outset. Chapter 2 takes what you discover in Chapter 1 and shows you how to apply it in the form of practical LINQ queries.

Chapter 3 is special — it tells you about the unique features in .NET Framework that make LINQ a workable solution. You’ll probably find yourself referring to this chapter as you read the rest of the book.

Chapters 4 and 5 provide exclusive information for Visual Basic.NET and Visual Studio 2005 developers. It seems as if these two groups get left out of most books, but you’ll find them here.

Chapter 1

Getting to Know LINQ

In This Chapter

Defining LINQ uses, benefits, and design goals

Considering the real world uses of LINQ

Defining declarative programming languages

Understanding the LINQ namespaces

The Language INtegrated Query (LINQ) feature of Visual Studio 2008 provides you with a new way to interact with data of all types. In fact, this new feature provides you with tools that make it easier to create queries using less code. The resulting queries are often easier to understand than other techniques for deriving information from both standard (think databases) nonstandard (think memory data structures) data sources. In addition, you gain a measure of flexibility that most developers associate with using a database, not lists provided internally as part of applications.

The easiest way to think of LINQ at the outset is as a means of looking for something — a specialized kind of search. Because most people are inundated with information today, providing a fast means of locating specific data is important. LINQ provides the means to perform a search without writing a lot of code. Everything is built in to the development environment so all you need to consider is what to find, not how to find it. Unlike other kinds of searches, however, LINQ provides the means to look inside data structures that you normally can’t search, such as objects. It can also standardize the methods you use to perform searches within Web services. In short, LINQ

Provides access to a huge range of data

Lets you simplify searches to locate just what you need

Reduces the code required to perform a search

Enables you to focus on the search instead of writing search routines

Interacts with all kinds of data sources using a standardized approach

This chapter serves as an introduction to LINQ. You discover how LINQ will make your coding experience better, reduce real world complexity, and make searches more accurate. As part of discovering LINQ, you also need to know about declarative languages, and this chapter provides the information you need. Finally, since LINQ is part of the .NET Framework, you need to know which namespaces support it, so this chapter provides an introduction to these new namespaces.

Considering LINQ

LINQ is possibly the most exciting new feature Microsoft has added to Visual Studio 2008. Sure, the other features that Microsoft added are important, but they don’t have the overwhelming reach of LINQ to change the way developers write applications. Anyone can use LINQ to create a better application — one that works more efficiently and uses less code. In addition, you no longer have to write custom search routines that differ from developer to developer. By using LINQ to perform searches of all types, you can standardize another part of your code base and incrementally improve overall developer productivity. The following sections describe LINQ in greater detail.

Understanding the task that LINQ performs

LINQ is all about searching efficiently and consistently. Your application searches efficiently by performing the task using less code and obtaining the results faster. Consistency comes from using the same code pattern to perform a search no matter what source of data you want to work with. From a pattern perspective, a search of an array looks the same as a search of a Web service or SQL Server database. Using LINQ, it no longer matters whether the data resides in SQL Server or MySQL, or even both. LINQ does divide queries into four common types (using different providers) that augment the basic patterns described in Chapter 2:

LINQ to Object

LINQ to DataSet

LINQ to SQL

LINQ to XML

It’s possible to have other kinds of “LINQ to” scenarios by adding other libraries. For example, you can find a LINQ to Active Directory library at http://www.codeplex.com/LINQtoAD. The goal, however, is to perform as many tasks as possible using the four basic LINQ to strategies provided with .NET Framework 3.5.

The most important thing to remember about LINQ is that it isn’t technology specific. This book shows you how to “LINQ to” any number of data sources, some of which you’ll find unusual because you may not have thought to search them before. For example, you may have an assortment of data in an object that you need to search — LINQ is the perfect tool for performing this task.

In addition to finding data, LINQ can also help organize it so that you present the user with only the data needed as output. Using special features of LINQ, you can filter data so that the user sees just the desired elements. You can also group and sort the data so that the user sees it in an order that makes it easier to use the data. The essential task that LINQ performs, therefore, is to make the data accessible. The user sees only the data needed and in the most productive way.

However, LINQ goes beyond searching for and ordering data. In many cases, you can also use LINQ to create a query that manipulates data in various ways, assuming the data source allows such manipulation. For example, you can use LINQ to change the content of a SQL Server database. Some unusual data sources such as Active Directory also allow modification. In fact, you can modify any configuration database that relies on the Lightweight Directory Access Protocol (LDAP). Consequently, the techniques in this book show you how to work with mainstream products, but you can easily modify them to meet any need.

Contemplating why you need LINQ

With feature bloat running rampant and developer time in ever limited quantity, you may wonder whether LINQ is the right choice for you. In most cases, you’d look for a list of qualifiers describing the technology and use these qualifiers to decide whether a technology is the right one for you. LINQ is a well-designed technology that can apply to anyone’s search needs — no qualifiers needed. You won’t have to wait for drivers or additional software to use it. In short, anyone who searches for data can use LINQ to meet that need.

Of course, now you’re thinking that this book is offering you the fabled silver bullet solution. LINQ isn’t a silver bullet. The other tools you have for searching are still useful and you’ll need to employ them. For example, even Microsoft admits that LINQ can have performance problems when searching SQL Server databases. You can read the five-part blog series about performance issues at http://blogs.msdn.com/ricom/archive/2007/06/22/dlinq-linq-to-sql-performance-part-1.aspx to obtain a good overview of the problems (but not any significant solutions). Chapter 11 provides you with information about how you can overcome performance issues (here’s where you find the solutions), so make sure to check it out as well.

It’s important to understand that LINQ works with most data sources but not all of them. For example, you can easily use LINQ with most public Web services and some private Web services. However, even though you can use LINQ with the Amazon, Google, and AOL Web services, it doesn’t work with the eBay Web services due to security concerns. In short, specific Web service requirements can prevent LINQ from working properly. You can find a complete list of LINQ providers (LINQ to solutions) at http://blogs.msdn.com/charlie/archive/2006/10/05/Links-to-LINQ.aspx. Table 1-1 shows a list of the providers as of this writing.

Table 1-1 LINQ Providers

LINQ to Solution

URL

LINQ Extender (toolkit for building LINQ providers)

http://www.codeplex.com/LinqExtender

LINQ over C# project

http://www.codeplex.com/LinqOverCSharp

LINQ to Active Directory

http://www.codeplex.com/LINQtoAD

LINQ to Amazon

http://weblogs.asp.net/fmarguerie/archive/2006/06/26/Introducing-Linq-to-Amazon.aspx

LINQ to Bindable Sources (SyncLINQ)

http://paulstovell.net/blog/index.php/why-synclinq-should-matter-to-you/

LINQ to CRM (Customer Relationship Management)

http://www.codeplex.com/LinqtoCRM

LINQ to Excel

http://www.codeplex.com/xlslinq

LINQ to Expressions (MetaLinq)

http://www.codeplex.com/metalinq

LINQ to Flickr

http://www.codeplex.com/LINQFlickr

LINQ to Geo (geospatial data)

http://www.codeplex.com/LinqToGeo

LINQ to Google

http://www.codeplex.com/glinq

LINQ to Indexes

http://www.codeplex.com/i4o/Release/ProjectReleases.aspx?ReleaseId=3519

LINQ to IQueryable

http://blogs.msdn.com/mattwar/archive/2007/08/09/linq-building-an-iqueryable-provider-part-vi.aspx

LINQ to JavaScript

http://www.codeplex.com/JSLINQ

LINQ to JSON (JavaScript Object Notation)

http://james.newtonking.com/archive/2008/02/11/linq-to-json-beta.aspx

LINQ to LDAP (Lightweight Directory Access Protocol)

http://community.bartdesmet.net/blogs/bart/archive/2007/04/05/the-iqueryable-tales-linq-to-ldap-part-0.aspx

LINQ to Lucene

http://www.codeplex.com/linqtolucene

LINQ to Metaweb (freebase)

http://www.codeplex.com/metawebToLinQ

LINQ to MySQL, Oracle, and PostgreSql

http://code2code.net/DB_Linq/

LINQ to NHibernate

http://www.ayende.com/Blog/archive/2007/03/17/Implementing-Linq-for-NHibernate-A-How-To-Guide--Part.aspx

LINQ to RDF (Resource Description Framework) Files

http://blogs.msdn.com/hartmutm/archive/2006/07/24/677200.aspx

LINQ to SharePoint

http://www.codeplex.com/LINQtoSharePoint

LINQ to SimpleDB

http://www.codeplex.com/LinqToSimpleDB

LINQ to Streams

http://www.codeplex.com/Slinq/

LINQ to WebQueries

http://blogs.msdn.com/hartmutm/archive/2006/06/12/628382.aspx

LINQ to WMI (Windows Management Instrumentation)

http://bloggingabout.net/blogs/emile/archive/2005/12/12/10514.aspx, http://tomasp.net/blog/linq-expand.aspx, and http://tomasp.net/blog/linq-expand-update.aspx

LINQ to XtraGrid

http://cs.rthand.com/blogs/blog_with_righthand/archive/2008/02/23/LINQ-to-XtraGrid.aspx

Don’t get the idea that LINQ always requires a provider. The provider does make it easier to perform tasks, but you can also create your own interface using the generic providers. For example, you can interact with Office 2007 files without using a specific provider. Chapter 10 shows you how to perform this task.

Even with these few warts, however, LINQ is a good solution for many search needs and you should at least try it. You need LINQ because it has so much to offer and doesn’t require a lot of time to master or use. LINQ provides the experimental platform that most developers crave. In those few situations where LINQ can’t do a good job for you, experimentation can at least help you understand the data source better.

Defining the LINQ design goals

Microsoft had a number of design goals in mind when it created LINQ. These design goals affect how you view LINQ today and how you can use it to solve specific application development problems. The following list describes the design goals.

Data source access simplification: One of the major issues of working with any data source is that the developer must know several disciplines to perform the task of accessing the data. For example, when working with SQL Server, the developer must understand the nuances of the base programming language, a database provider, and a language such as SQL to obtain access to the data. If the developer decides to access XML data, it’s necessary to master an entirely different set of disciplines. LINQ overcomes this problem by providing a single method of accessing data.

Data manipulation simplification: When you make a query using C# or Visual Basic .NET, you have to worry about the structure of the data source. For example, when you query SQL Server, you must consider the tables, indexes, views, and other structural elements of the database. The use of these structural elements is necessary but not helpful. You end up thinking about the data structure and not the data. Consequently, many developers create convoluted and difficult to understand data-manipulation code when what they really wanted was the data (the underlying structure isn’t important).

Data translation: In most cases, you must write special routines to move data from one data source to another. For example, if you want to move data from an XML file to SQL Server, you must perform some special tasks to do it. In addition, moving the data doesn’t always provide the results you expected. Differences in data source capabilities make the translation less than perfect. LINQ reduces the complexity of data translation significantly. It doesn’t always provide a perfect translation either, but you’ll find that the translation is usually better because each provider performs the required translation for you.

Object mapping: Most programming languages today rely on some form of object orientation. Objects have special characteristics that you won’t find in many data sources. For example, an object doesn’t respect the tables, indexes, and other data structures found in SQL Server. Consequently, you need a means of mapping the object to the data source and vice versa. In the past, the developer had to rely on complex objects that Visual Studio generated for them. Using LINQ provides object mapping without the complexity.

Language extensibility: Microsoft provides a limited number of “LINQ to” providers as part of the .NET Framework. These providers are capable, but they don’t address every need. Consequently, one of the design goals for LINQ is to provide language extensibility so that third parties can create other providers. Table 1-1 shows an example of just how many providers have already been created by third parties, and you can expect more in the future.

Multiple data source extensibility: Because one of the goals for LINQ is data translation, it’s important to have providers that can work with multiple data sources. The goal is to make it possible to move data from any data source to any other data source. In addition, Microsoft wants LINQ to be able to use data from any data source and combine it with data from any other data source to create a composite output.

Type safety: A major problem with many data source usage scenarios today is that data problems are discovered only at run time, often without any help from the language product or the application. A developer may not discover a problem until someone complains about mangled data. The type safety features of LINQ help you discover potential data problems during compile time, when they’re easy to fix, rather than getting your bad news later.

IntelliSense support: Creating a query using standard development tools can be hit or miss. LINQ provides IntelliSense support so that you can see how to create the query as you create it.

Debugger support: Due to strong typing and other features of LINQ, you get full debugger support, which makes finding a particular problem considerably easier. No longer do you have to look for that errant bit of code in a loop or the missed type issue in a custom class. LINQ helps you diagnose problems quickly and easily.

Older product support: Even though most of this book uses new technology that Microsoft provides, you can use LINQ with older products as well. Obviously, the support isn’t built in to these older products, so LINQ doesn’t work as seamlessly. (You must use at least the .NET Framework 2.0.)

Backward compatibility: One of Microsoft’s major goals was to ensure that you could continue using all the data structures you used in the past. LINQ simply provides a different way to interact with those data structures.

Using LINQ with other languages

Don’t get the idea that LINQ is going to remain a solution for C# and Visual Basic .NET developers alone. Using LINQ does require the use of a different compiler, but that won’t stop other languages from employing it. You can already find support for LINQ in Microsoft’s new F# language and you’ll probably find it in use with C++ as well.

LINQ will appear as part of other language packages in the future. There are rumors that Borland Delphi will also have LINQ support at some time (read more at http://www.eweek.com/c/a/Application-Development/Borland-Plans-to-Support-MS-LINQ-in-Delphi-Platform/), and you can expect that other languages such as PHP will have it as well. If you want to find out more about languages that will support LINQ, check out Charlie Calvert’s Community Blog at http://blogs.msdn.com/charlie/archive/2006/10/05/Links-to-LINQ.aspx.

Understanding the LINQ requirements

LINQ is part of .NET Framework 3.5. Consequently, you need Visual Studio 2008 to work with LINQ effectively. This book assumes that you have a copy of Visual Studio 2008 installed on your system. The examples rely on Visual Studio 2008 Professional Edition and you may not get precisely the same results when you use a different edition of the product. Theoretically, you could use LINQ with Visual Studio 2005 (Chapter 5 discusses this technique), but the bulk of this book relies on Visual Studio 2008.

Microsoft has also decided to focus attention on C# as the programming language of choice when using LINQ. C# provides a few extensions and features that make working with LINQ easier. However, you can use Visual Basic .NET quite well with LINQ, too. Although most of the techniques in this book work with any language you want to use, the example code appears in C#. The exception is Chapter 4, which shows how to work with LINQ with Visual Basic .NET. These examples will help you to apply any of the examples to the Visual Basic .NET environment.

The use of C# begs the question of what makes it so special. Chapter 3 describes the .NET Framework extensions that make working with LINQ considerably easier. Some of these language extensions are found only in C# and others are easier to work with in C#. You find a complete description of the features and how to use them in the chapter. For now, all you need to know is that this book will help you use LINQ no matter which language you choose and what platform you have. It’s also important to know that the majority of the book is focused on Visual Studio 2008 C# developers because this is the group that Microsoft has chosen as its target group for LINQ.

Using LINQ in the Real World

Realistically, using LINQ is possibly overkill if your goal is to search through a short list of items found in a control. Most developers will use LINQ for something a little more complex than simple lists (then again, nothing stops you from using LINQ even for simple tasks — it’s that fast and easy). You know from previous sections of the chapter that LINQ isn’t a silver bullet solution. The technology has problems with security and you may not always find the performance stellar, so it’s important to weigh the cost of using LINQ against the benefits it provides, which are substantial.

Many developers will likely begin using LINQ in places where they don’t currently have a good solution, such as with Web services, or in situations where they already know how to perform a search, such as with SQL Server. The starting goal is to discover how well LINQ works to perform a basic query and then move on to something more complicated. Developers will want to kick the tires for a while and then discover that LINQ really does do powerful things with only a little code.

The most exciting use of LINQ is to perform data translation. Currently, a query of Amazon’s Web service, interpretation, and translation into a form that SQL Server will use can require several hundred lines of code. I know this from experience because I’ve written such code in the past. It’s nice to make two queries with LINQ that require perhaps twenty lines of code to perform the same task. As development environments become more complex and the number of data sources increase, developers will need the special talents of LINQ to perform their data translation for them.

It doesn’t take long to realize that LINQ in the real world is all about getting the job done fast, reliably, and with fewer lines of code. In addition, the simplification that LINQ provides makes it possible to use data sources even when you aren’t completely familiar with them. For example, a developer who normally works with SQL Server would need training to work with MySQL because the two products have differences. Because LINQ hides the differences between these two products, a SQL Server developer could possibly work with MySQL with little, if any, training. All that the developer would need to do is to ensure that the query is formed correctly — and IntelliSense even helps with that issue. In short, the real world view of LINQ is that it makes developers incredibly productive.

LINQ query testing is required

LINQ is a new technology. As such, it’s tempting to look at the benefits and say that it’s the new perfect tool or to look at the deficiencies and proclaim another Microsoft failure. However, after you begin working with LINQ, you begin to understand that LINQ is neither of these viewpoints — it’s simply a new tool to put in your arsenal. Many developers will find that LINQ is one very good answer to specific needs, but as with any tool, it has limitations.

The problem now is that because LINQ is a new tool, you don’t know anything about its limitations. This book presents a considerable number of examples, and it’s a good idea to try them all. However, at some point, you’re going to have to test LINQ against the tools you currently use or should use. For example, you should probably test LINQ against your current .NET code and the SQL Server stored procedures that you use. In some cases, LINQ is most definitely a winner, but in other cases, you’ll want to stick with existing technologies.

A rule of thumb for LINQ is that it simplifies queries. If your goal is to simplify the task of querying a data source, LINQ is normally going to come out ahead. Because LINQ uses a standardized method to create a query, it’s simple to learn, and that can also make it considerably much more reliable than existing technologies. Developers are less likely to make errors when they have a tool that makes writing code easy. The fact that LINQ queries are generally shorter than any code you can write also tends to reduce errors and make code more reliable. However, because you’re depending on LINQ to determine how to perform a particular task, LINQ doesn’t always provide the required performance, which is why you must test any solution you create against the existing model (when one exists).

Understanding Declarative Programming

Most Visual Studio developers already understand imperative languages because C# and Visual Basic .NET are imperative languages. An imperative language describes how to solve a particular problem. You use an imperative language to write a procedure to answer a specific need. A user clicks a button and the button click event handler provides a procedure to respond.

Imperative languages assume that you know how to solve a problem, and in most cases, you do. However, sometimes you don’t know how to solve a problem or the language itself has gaps that make a solution difficult. Obtaining data from a data source is one of those problems. In this case, you need a declarative language such as LINQ. When using a declarative language, you state the problem and let the language decide how to solve the problem. Other kinds of declarative languages include the Structured Query Language (SQL) used in SQL Server. In fact, you’ll find that LINQ has similarities to SQL, even though the two languages aren’t directly compatible.

Declarative languages can be divided into several groups, including logic, functional, and query languages. LINQ is in the query language group. Microsoft’s new F# language is in the functional group (see my article at http://www.devsource.com/cp/bio/John-Paul-Mueller/). No matter which group a declarative language is in, the basic assumption is the same: A developer provides a problem and the language provides the method for solving that problem. In short, a declarative language defines a relationship between a problem and its solution.

Part of the strength of LINQ is that you can combine it with an imperative language such as C# or Visual Basic .NET to create a stronger whole. Using LINQ lets you rely on the language itself to solve certain problems, such as how to obtain the data you specify from a particular data source.

Despite Microsoft’s declarations to the opposite, LINQ, like SQL, isn’t a pure declarative language. For example, you can include functions as part of a LINQ query, so the language doesn’t necessarily define a pure relationship between a problem and its solution — you can tweak the solution using the function. In addition, the order in which you define the problem affects the solution. These deviations from a pure declarative language are necessary to ensure that you receive the proper outcome of a query. For the purposes of this book, LINQ is a declarative language in the query language group that interacts with the C# or Visual Basic .NET imperative languages.

An Overview of the LINQ Namespaces

Microsoft chose not to provide a single LINQ namespace. The .NET Framework has a number of LINQ namespaces, each of which creates a different kind of data connection. The following sections describe the higher level LINQ namespaces in .NET Framework 3.5. You could find other LINQ libraries on the Internet for use with other connection types. Chapter 12 discusses one such library that you can use for accessing Active Directory.

System.Linq namespace

The System.Linq namespace contains all basic classes and interfaces that you use to work with LINQ. Every LINQ to solution relies on this namespace for basic support. As you’ll see in later chapters, this is the one namespace that you always include when you want to use LINQ. The samples in Chapter 2 show initial usage of this namespace and you’ll also see it in Chapters 3 through 5. Chapter 6 begins the full examination of this namespace as part of working with LINQ to objects. You can find out more about this namespace at http://msdn2.microsoft.com/en-us/library/system.linq.aspx.

System.Linq.Expressions namespace

The System.Linq.Expressions namespace contains the classes, interfaces, and enumerations used to create expressions. An expression is essentially a tree of nodes that define how a query works. For example, you can create a binary expression that defines how to subtract one number from another. A constant expression can define a constant value, and a named parameter expression can define a value that receives data of a particular type. The essential expression types are

BinaryExpression

ConditionalExpression

ConstantExpression

InvocationExpression

LambdaExpression

ListInitExpression

MemberExpression

MemberInitExpression

MethodCallExpression

NewArrayExpression

NewExpression

ParameterExpression

TypeBinaryExpression

UnaryExpression

Chapter 3 begins the discussion of several expression types, but you’ll find expressions used throughout this book. You can find out more about this namespace at http://msdn2.microsoft.com/en-us/library/system.linq.expressions.aspx.

System.Data.Linq namespace

The System.Data.Linq namespace contains the classes, structures, interfaces, and enumerations used for SQL database interactions. This is the basic namespace used for LINQ to SQL scenarios. It’s important to remember that this is LINQ to SQL and not LINQ to SQL Server. The classes in this namespace help you perform a number of data manipulation tasks, including:

SELECT data from the database

UPDATE data found in the database

DELETE records as needed in the database

Interact with binary data

Use and implement referential integrity rules

Work with a particular table or other database objects (such as indexes)

Translate the data from one data source to another

You’ll find that you use this namespace for a number of LINQ to SQL scenarios. Chapter 8 begins the discussion of working with LINQ to SQL Server. Chapter 11 discusses a number of advanced LINQ to SQL Server topics.

This namespace comes into play also when you work with DataSet objects. A DataSet needs to work exclusively with an external data source; you can also use it to work with internal data sources, so this is an extremely flexible namespace. Discussions in Chapter 7 show how you can use LINQ to DataSet to work with DataSet objects in your application. Look at Chapter 13 if you want to see how this namespace can affect other LINQ to scenarios, such as LINQ to MySQL. You can find out more about this namespace at http://msdn2.microsoft.com/en-us/library/system.data.linq.aspx.

System.Data.Linq.Mapping namespace

The System.Data.Linq.Mapping namespace contains the classes and enumerations to map data between an imperative language such as C# or Visual Basic .NET and a declarative language such as SQL. It also comes into play when working with technologies such as XML. In short, you’ll use this class when working with any external data source that has a different representation from the standard object-oriented view of data found in the .NET Framework. Coverage of this namespace begins in Chapter 7, but you’ll find it used throughout the book. You can find out more about this namespace at http://msdn2.microsoft.com/en-us/library/system.data.linq.mapping.aspx.

System.Data.SqlClient namespace

The System.Data.SqlClient namespace contains the classes used to create a basic connection with SQL Server. Although you might use this namespace in a number of scenarios, you’ll generally use it exclusively with SQL Server. The classes in this namespace help you perform the following tasks:

Interact with SQL Server 2000

Interact with SQL Server 2005

Perform string pattern matching

Perform data manipulation, especially with dates

Create a basic SQL Server connection

The coverage of this namespace begins in Chapter 8, but you’ll also find advanced features described in Chapter 11. You can find out more about this namespace at http://msdn2.microsoft.com/en-us/library/system.data.linq.sqlclient.aspx.

System.Data.SqlClient.Implementation namespace

The System.Data.SqlClient.Implementation namespace contains the class used to implement the SQL Server client functionality found in the System.Data.SqlClient namespace. You generally won’t use the classes found in this namespace directly. The coverage of this namespace begins in Chapter 8, but you’ll also find advanced features described in Chapter 11. You can find out more about this namespace at http://msdn2.microsoft.com/en-us/library/system.data.linq.sqlclient.implementation.aspx.

System.Xml.Linq namespace

The System.Xml.Linq namespace contains classes and enumerations used to interact with XML data of all type. When you think about the number of ways in which modern computer systems use XML data, this namespace covers a significant amount of ground. As with SQL Server, you can use the classes of this namespace to interact with XML files in a number of ways. The following list provides an overview of the kinds of interaction you can perform:

Load XML from files or streams (basic input functionality)

Serialize XML to files or streams (basic output functionality)

Create XML trees from scratch using functional construction (data translation)

Query XML trees using LINQ queries (basic data viewing and manipulation)

Manipulate in-memory XML trees (advanced data manipulation)

Validate XML trees using XSD (data verification)

Combine all of these features to perform advanced data translation tasks and even move data to other data sources