You are previewing LINQ For Dummies®.
O'Reilly logo
LINQ For Dummies®

Book Description

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!

    Table of Contents

    1. Copyright
    2. About the Author
    3. Author's Acknowledgments
    4. Introduction
      1. About This Book
      2. Conventions Used in This Book
      3. What You Should Read
      4. What You Don't Have to Read
      5. Foolish Assumptions
      6. How This Book Is Organized
        1. Part I: An Overview of LINQ
        2. Part II: Using Standard LINQ to Technologies
        3. Part III: Extending LINQ to New Horizons
        4. Part IV: The Part of Tens
        5. The accompanying Web site
      7. Icons Used in This Book
      8. Where to Go from Here
    5. I. An Overview of LINQ
      1. 1. Getting to Know LINQ
        1. 1.1. Considering LINQ
          1. 1.1.1. Understanding the task that LINQ performs
          2. 1.1.2. Contemplating why you need LINQ
          3. 1.1.3. Defining the LINQ design goals
          4. 1.1.4. Understanding the LINQ requirements
        2. 1.2. Using LINQ in the Real World
        3. 1.3. Understanding Declarative Programming
        4. 1.4. An Overview of the LINQ Namespaces
          1. 1.4.1. System.Linq namespace
          2. 1.4.2. System.Linq.Expressions namespace
          3. 1.4.3. System.Data.Linq namespace
          4. 1.4.4. System.Data.Linq.Mapping namespace
          5. 1.4.5. System.Data.SqlClient namespace
          6. 1.4.6. System.Data.SqlClient.Implementation namespace
          7. 1.4.7. System.Xml.Linq namespace
      2. 2. Creating Simple LINQ Queries
        1. 2.1. Defining the LINQ Keywords
          1. 2.1.1. Working with from
          2. 2.1.2. Working with where
          3. 2.1.3. Working with orderby
          4. 2.1.4. Working with join
          5. 2.1.5. Working with let
        2. 2.2. Creating a Simple from Query
          1. 2.2.1. Understanding the simple query application code
          2. 2.2.2. Using the debugger to see the simple query in action
        3. 2.3. Filtering a Query
        4. 2.4. Grouping Queries
        5. 2.5. Creating Queries from Multiple Sources
          1. 2.5.1. Understanding the multiple-source application code
          2. 2.5.2. Working with the alternative multiple-source example
          3. 2.5.3. Using calculated values in multiple-source queries
        6. 2.6. Working with the Standard Query Operators
        7. 2.7. Mapping Keywords to Methods
      3. 3. Considering the .NET Language Extensions
        1. 3.1. An Overview of the Language Extensions
        2. 3.2. Working with Lambda Expressions
        3. 3.3. Understanding the Role of the IEnumerable and IEnumerable <T> Interfaces
        4. 3.4. Understanding Object Initializers
        5. 3.5. Understanding Collection Initializers
        6. 3.6. Working with Extension Methods
        7. 3.7. Working with Partial Methods
        8. 3.8. An Overview of Expression Trees
        9. 3.9. Working with Query Expressions
      4. 4. Working with LINQ in Visual Basic .NET
        1. 4.1. Understanding the Visual Basic Differences
        2. 4.2. Creating the Simple Visual Basic Example
          1. 4.2.1. Working with From
            1. 4.2.1.1. Understanding the code
            2. 4.2.1.2. Using the debugger to see the code execute
          2. 4.2.2. Working with Where
          3. 4.2.3. Working with Order By
          4. 4.2.4. Working with Join
          5. 4.2.5. Working with Let
        3. 4.3. Using the Additional Visual Basic Keywords
          1. 4.3.1. Working with Aggregate
          2. 4.3.2. Working with Distinct
          3. 4.3.3. Working with Skip
          4. 4.3.4. Working with Take
        4. 4.4. Working with Lambda Functions in Visual Basic
      5. 5. Working with LINQ in Visual Studio 2005
        1. 5.1. Obtaining LINQ Support for Visual Studio 2005
        2. 5.2. Installing LINQ Support
        3. 5.3. Creating the Simple Visual Studio 2005 Project
          1. 5.3.1. Defining the project
          2. 5.3.2. Working with from
            1. 5.3.2.1. Understanding the code
            2. 5.3.2.2. Using the debugger
          3. 5.3.3. Working with join
          4. 5.3.4. Working with where
          5. 5.3.5. Working with orderby
          6. 5.3.6. Working with let
    6. II. Using Standard LINQ to Technologies
      1. 6. LINQ to Object
        1. 6.1. Considering the Use of Objects with LINQ
        2. 6.2. Creating the Simple Object Query Example
        3. 6.3. Understanding the Role of Deferred Operators
        4. 6.4. Creating the Deferred Operator Examples
          1. 6.4.1. Working with Concat
          2. 6.4.2. Working with AsEnumerable, AsQueryable, Cast, and OfType
          3. 6.4.3. Working with OfType and Where
          4. 6.4.4. Working with DefaultIfEmpty, Empty, Range, and Repeat
          5. 6.4.5. Working with GroupBy and ToLookup
          6. 6.4.6. Working with GroupJoin and Join
          7. 6.4.7. Working with Skip, SkipWhile, Take, and TakeWhile
          8. 6.4.8. Working with Select and SelectMany
          9. 6.4.9. Working with Distinct, Except, Intersect, and Union
          10. 6.4.10. Working with OrderBy, OrderByDescending, Reverse, ThenBy, and ThenByDescending
        5. 6.5. Understanding the Role of Nondeferred Operators
        6. 6.6. Creating the Nondeferred Operator Examples
          1. 6.6.1. Working with Aggregate, Average, Count, LongCount, Max, Min, and Sum
          2. 6.6.2. Working with ToArray, ToDictionary, ToList, and ToLookup
          3. 6.6.3. Working with ElementAt, ElementAtOr Default, First, FirstOrDefault, Last, LastOrDefault, Single, and SingleOrDefault
          4. 6.6.4. Working with SequenceEqual
          5. 6.6.5. Working with All, Any, and Contains
      2. 7. LINQ to DataSet
        1. 7.1. Considering the Use of DataSets with LINQ
        2. 7.2. Creating the Simple DataSet Example
          1. 7.2.1. Defining the test tables
          2. 7.2.2. Outputting the results
          3. 7.2.3. Exploring the connection
        3. 7.3. Understanding the LINQ to DataSet Operators
        4. 7.4. Creating the Filtered Output Example
          1. 7.4.1. Creating a simple display
          2. 7.4.2. Modifying the filtered output
          3. 7.4.3. Using the CopyToDataTable operator
          4. 7.4.4. Working with typed DataSets
            1. 7.4.4.1. Defining the DataSet
            2. 7.4.4.2. Working with the typed DataSet
        5. 7.5. Working with Multiple DataSet Tables
      3. 8. LINQ to SQL Server
        1. 8.1. Considering the Use of SQL Server with LINQ
        2. 8.2. Obtaining and Installing the Northwind Database
          1. 8.2.1. Downloading the database
          2. 8.2.2. Installing the database
          3. 8.2.3. Testing the Visual Studio connection
          4. 8.2.4. Generating the Northwind entity classes and XML mapping files
        3. 8.3. Creating the Simple SQL Server Query Example
          1. 8.3.1. Overcoming the Visual Studio 2008 connectivity issues
          2. 8.3.2. Defining the project
          3. 8.3.3. Adding the code
          4. 8.3.4. Viewing the debugger output
        4. 8.4. Using Object Relational Designer
        5. 8.5. Understanding the LINQ to SQL Server Operators
      4. 9. LINQ to XML
        1. 9.1. Considering the Use of XML with LINQ
        2. 9.2. Working with the LINQ to XML API
        3. 9.3. Understanding the LINQ to XML Operators
          1. 9.3.1. Working with XDeclaration, XElement, and XDocument
          2. 9.3.2. Working with XNamespace
          3. 9.3.3. Working with XProcessingInstruction
          4. 9.3.4. Working with XAttribute
          5. 9.3.5. Working with XComment and XText
          6. 9.3.6. Working with XCData
          7. 9.3.7. Working with XDocumentType
          8. 9.3.8. Working with XNodeDocumentOrderComparer
          9. 9.3.9. Working with XNodeEqualityComparer
          10. 9.3.10. Working with Remove
          11. 9.3.11. Working with XName
        4. 9.4. Creating the Simple XML File Example
          1. 9.4.1. Creating the project
          2. 9.4.2. Building an XML document
          3. 9.4.3. Saving an XML document
          4. 9.4.4. Loading an XML document
    7. III. Extending LINQ to New Horizons
      1. 10. Using LINQ with Office 2007
        1. 10.1. Understanding the Office Document Structure
        2. 10.2. Understanding LINQ Interaction with Office
        3. 10.3. Obtaining the Required Library
        4. 10.4. Creating the Office 2007 Document Example
          1. 10.4.1. Creating the project
          2. 10.4.2. Understanding the custom properties
          3. 10.4.3. Reading document properties
          4. 10.4.4. Writing document properties
      2. 11. Advanced LINQ to SQL Server
        1. 11.1. Considering SQL Server Issues
          1. 11.1.1. Understanding concurrency problems
            1. 11.1.1.1. Understanding the problem and its resolution
            2. 11.1.1.2. Seeing concurrency checks and resolution in action
            3. 11.1.1.3. Defining alternatives to ResolveAll()
            4. 11.1.1.4. Using entity class attributes to modify concurrency detection
          2. 11.1.2. Overcoming performance issues
            1. 11.1.2.1. Using Column attributes
            2. 11.1.2.2. Relying on standard database performance tricks
            3. 11.1.2.3. Performing database updates only as needed
            4. 11.1.2.4. Using eager loading
        2. 11.2. Creating the Database Modification Example
          1. 11.2.1. Performing an insert
          2. 11.2.2. Performing an update
          3. 11.2.3. Performing a delete
        3. 11.3. Using Concurrency Checks and Resolving Errors
          1. 11.3.1. Resolving conflicts at the object level
          2. 11.3.2. Resolving conflicts at the member level
      3. 12. LINQ to Active Directory
        1. 12.1. Working with Specific Active Directory Objects
        2. 12.2. Defining Active Directory Variables
        3. 12.3. Obtaining the LINQ to Active Directory Provider
        4. 12.4. Creating the Simple Active Directory Query Example
          1. 12.4.1. Performing the project setup
          2. 12.4.2. Defining an Active Directory object class
          3. 12.4.3. Creating a root node pointer
          4. 12.4.4. Reading user information
          5. 12.4.5. Writing user information
        5. 12.5. Understanding the Limitations of Active Directory Interaction
          1. 12.5.1. Some LINQ methods aren't implemented
          2. 12.5.2. Defining the need for LDAP patience
      4. 13. Other LINQ to Strategies
        1. 13.1. Understanding the Qualifications for a LINQ to Solution
        2. 13.2. Accessing COM+ Using LINQ
          1. 13.2.1. COM+ accessed as a Web service
          2. 13.2.2. COM+ accessed using interop functionality
        3. 13.3. Creating the Resource Description Format (RDF) Files Example
          1. 13.3.1. A quick overview of RDF
          2. 13.3.2. A quick overview of SPARQL
          3. 13.3.3. Starting the project
          4. 13.3.4. Configuring the project
            1. 13.3.4.1. Defining the client project
            2. 13.3.4.2. Defining the host project
          5. 13.3.5. Creating the host application
          6. 13.3.6. Creating the application class definition
          7. 13.3.7. Reading RDF files
        4. 13.4. Creating the MySQL Example
          1. 13.4.1. Getting the MySQL ADO.NET connector
          2. 13.4.2. Compiling the DbLinq.DLL and DbLinq.MySQL.DLL files
          3. 13.4.3. Configuring the database
          4. 13.4.4. Defining the MySQL project
          5. 13.4.5. Developing the query code
    8. IV. The Part of Tens
      1. 14. Ten Ways to Improve LINQ Development
        1. 14.1. Using LINQ Tools to Simplify Coding
          1. 14.1.1. Getting the VLinq add-in application
          2. 14.1.2. Creating a VLinq query
          3. 14.1.3. Using a VLinq query in an application
        2. 14.2. Using LINQ to Create Self Documenting Code
        3. 14.3. Analyzing Code Patterns
        4. 14.4. Querying a Code Snippet Database
        5. 14.5. Locating Other Development Resources
        6. 14.6. Using LINQ to Query Data Formats
        7. 14.7. Finding Usage Trends
        8. 14.8. Developing a LINQ Library
        9. 14.9. Sharing LINQ Queries with Others
        10. 14.10. Analyzing Compiler and IDE Output
      2. 15. Ten Ways to Reduce Application Support Costs
        1. 15.1. Creating Self-Modifying Queries
        2. 15.2. Using LINQ to Create Reports
        3. 15.3. Addressing User Search Needs
        4. 15.4. Creating User-Friendly Mashups
        5. 15.5. Making Help More Accessible
        6. 15.6. Organizing and Querying Support Requests and Responses
        7. 15.7. Developing Fast Searches from Multiple Sources
        8. 15.8. Helping Users Locate Existing Resources
        9. 15.9. Performing Support Task Automation
        10. 15.10. Improving Application and System Management
      3. 16. Ten LINQ Resources
        1. 16.1. Starting with the Microsoft Developer Network
        2. 16.2. Getting Tips from the Microsoft Blogs
        3. 16.3. Finding Help in Third-Party Web Sites
        4. 16.4. Finding Help in Third-Party Newsletters and Blogs
        5. 16.5. Using Other Sources for LINQ to Objects Projects
        6. 16.6. Using Other Sources for LINQ to SQL Server Projects
        7. 16.7. Using Other Sources for LINQ to XML Projects
        8. 16.8. Considering Other LINQ to Sources Projects
        9. 16.9. Getting Help with Visual Basic Projects
        10. 16.10. Getting Help with C# Projects