You are previewing Programming Microsoft® LINQ.
O'Reilly logo
Programming Microsoft® LINQ

Book Description

Get comprehensive guidance for using the Microsoft Language Integrated Query (LINQ) Project—with in-depth insights from two experienced developers. Data-rich applications can be difficult to create because of the tremendous differences between query languages used to access data and the programming languages commonly used to write applications. This practical guide covers the intricacies of LINQ, a set of extensions to the Visual C# and Visual Basic programming languages. Instead of traversing different language syntaxes required for accessing data from relational and hierarchical data sources, developers will learn how to write queries natively in Visual C# or Visual Basic—helping reduce complexity and boost productivity. Written by two experienced developers with strong ties to the developer teams at Microsoft, this book describes the LINQ architecture and classes, details the new language features in both Visual C# and Visual Basic, and provides code samples in both languages.

Table of Contents

  1. Programming Microsoft® LINQ
  2. Dedication
  3. A Note Regarding Supplemental Files
  4. Foreword
  5. Preface
    1. Acknowledgments
  6. Introduction
    1. About This Book
    2. System Requirements
    3. The Companion Web Site
    4. Support for This Book
  7. I. LINQ Foundations
    1. 1. LINQ Introduction
      1. What Is LINQ?
      2. Why Do We Need LINQ?
      3. How LINQ Works
        1. Relational Model vs. Hierarchical/Network Model
        2. XML Manipulation
      4. Language Integration
        1. Declarative Programming
        2. Type Checking
        3. Transparency Across Different Type Systems
      5. LINQ Implementations
        1. LINQ to Objects
        2. LINQ to ADO.NET
        3. LINQ to XML
      6. Summary
    2. 2. LINQ Syntax Fundamentals
      1. LINQ Queries
        1. Query Syntax
        2. Full Query Syntax
      2. Query Keywords
        1. From Clause
        2. Where Clause
        3. Select Clause
        4. Group and Into Clauses
        5. Orderby Clause
        6. Join Clause
        7. Let Clause
        8. Additional Visual Basic 2008 Keywords
      3. Deferred Query Evaluation and Extension Method Resolution
        1. Deferred Query Evaluation
        2. Extension Method Resolution
      4. Some Final Thoughts About LINQ Queries
        1. Degenerate Query Expressions
        2. Exception Handling
      5. Summary
    3. 3. LINQ to Objects
      1. Query Operators
        1. The Where Operator
        2. Projection Operators
          1. Select
          2. SelectMany
        3. Ordering Operators
          1. OrderBy and OrderByDescending
          2. ThenBy and ThenByDescending
          3. Reverse Operator
        4. Grouping Operators
        5. Join Operators
          1. Join
          2. GroupJoin
        6. Set Operators
          1. Distinct
          2. Union, Intersect, and Except
        7. Aggregate Operators
          1. Count and LongCount
          2. Sum
          3. Min and Max
          4. Average
          5. Aggregate
        8. Aggregate Operators in Visual Basic 2008
        9. Generation Operators
          1. Range
          2. Repeat
          3. Empty
        10. Quantifiers Operators
          1. Any
          2. All
          3. Contains
        11. Partitioning Operators
          1. Take
          2. TakeWhile
          3. Skip and SkipWhile
        12. Element Operators
          1. First
          2. FirstOrDefault
          3. Last and LastOrDefault
          4. Single
          5. SingleOrDefault
          6. ElementAt and ElementAtOrDefault
          7. DefaultIfEmpty
        13. Other Operators
          1. Concat
          2. SequenceEqual
      2. Conversion Operators
        1. AsEnumerable
        2. ToArray and ToList
        3. ToDictionary
        4. ToLookup
        5. OfType and Cast
      3. Summary
  8. II. LINQ to Relational Data
    1. 4. LINQ to SQL: Querying Data
      1. Entities in LINQ to SQL
        1. External Mapping
      2. Data Modeling
        1. DataContext
        2. Entity Classes
        3. Entity Inheritance
        4. Unique Object Identity
        5. Entity Constraints
        6. Associations Between Entities
          1. EntityRef
          2. EntitySet
          3. Graph Consistency
        7. Relational Model vs. Hierarchical Model
      3. Data Querying
        1. Projections
        2. Stored Procedures and User-Defined Functions
          1. Stored Procedures
          2. User-Defined Functions
        3. Compiled Queries
        4. Different Approaches to Querying Data
        5. Direct Queries
        6. Deferred Loading of Entities
        7. Deferred Loading of Properties
        8. Read-Only DataContext Access
        9. Limitations of LINQ to SQL
          1. Aggregate Operators
          2. Partitioning Operators
          3. Element Operators
          4. String Methods
          5. DateTime Methods
          6. Unsupported SQL Functionalities
      4. Thinking in LINQ to SQL
        1. The IN/EXISTS Clause
        2. SQL Query Reduction
        3. Mixing .NET Code with SQL Queries
      5. Summary
    2. 5. LINQ to SQL: Managing Data
      1. CRUD and CUD Operations
        1. Entity Updates
          1. Cascading Deletes and Updates
          2. Entity States
          3. Entity Synchronization
        2. Database Updates
          1. Overriding SubmitChanges
        3. Customizing Insert, Update, and Delete
          1. Stored Procedures
          2. Interception of Insert, Update, and Delete
      2. Database Interaction
        1. Concurrent Operations
          1. Column Attributes for Concurrency Control
        2. Transactions
        3. Exceptions
          1. DataContext Construction
          2. Database Reads
          3. Database Writes
          4. Entity Manipulation
      3. Database and Entities
        1. Entity Attributes to Maintain Valid Relationships
        2. Deriving Entity Classes
        3. Attaching Entities
          1. Entity Serialization
          2. Attach Operation
        4. Binding Metadata
          1. External XML Mapping File
          2. Creating a Database from Entities
          3. Creating Entities from a Database
        5. Differences Between .NET and SQL Type Systems
      4. Summary
    3. 6. Tools for LINQ to SQL
      1. File Types
        1. DBML—Database Markup Language
        2. C# and Visual Basic Source Code
        3. XML—External Mapping File
        4. LINQ to SQL File Generation
          1. Generating a DBML File from an Existing Database
          2. Generating an Entity's Source Code with Attribute-Based Mapping
          3. Generating an Entity's Source Code with an External XML Mapping File
          4. Creating a DBML File from Scratch
      2. SQLMetal
        1. Generating a DBML File from a Database
        2. Generating Source Code and a Mapping File from a Database
        3. Generating Source Code and a Mapping File from a DBML File
      3. Using the Object Relational Designer
        1. DataContext Properties
        2. Entity Class
          1. Entity Members
        3. Association Between Entities
          1. One-to-One Relationships
        4. Entity Inheritance
        5. Stored Procedures and User-Defined Functions
          1. Return Type of Data Function
          2. Mapping to Delete, Insert, and Update Operations
        6. Views and Schema Support
      4. Summary
    4. 7. LINQ to DataSet
      1. Introducing LINQ to DataSet
      2. Using LINQ to Load a DataSet
        1. Loading a DataSet with LINQ to SQL
        2. Loading Data with LINQ to DataSet
      3. Using LINQ to Query a DataSet
        1. Inside DataTable.AsEnumerable
        2. Creating DataView Instances with LINQ
        3. Using LINQ to Query a Typed DataSet
        4. Accessing Untyped DataSet Data
        5. DataRow Comparison
      4. Summary
    5. 8. LINQ to Entities
      1. Querying Entity Data Model
        1. Overview
        2. Query Expressions
      2. Managing Data
      3. Query Engine
        1. Query Execution
        2. More on ObjectQuery<T>
        3. Compiled Queries
      4. LINQ to SQL and LINQ to Entities
      5. Summary
  9. III. LINQ and XML
    1. 9. LINQ to XML: Managing the XML Infoset
      1. Introducing LINQ to XML
      2. LINQ to XML Programming
        1. XDocument
        2. XElement
        3. XAttribute
        4. XNode
        5. XName and XNamespace
        6. Other X* Classes
        7. XStreamingElement
        8. XObject and Annotations
      3. Reading, Traversing, and Modifying XML
      4. Summary
    2. 10. LINQ to XML: Querying Nodes
      1. Querying XML
        1. Attribute, Attributes
        2. Element, Elements
        3. XPath Axes "like" Extension Methods
        4. XNode Selection Methods
        5. InDocumentOrder
      2. Deferred Query Evaluation
      3. LINQ Queries over XML
        1. Querying XML Efficiently to Build Entities
      4. Transforming XML with LINQ to XML
      5. Support for XSD and Validation of Typed Nodes
      6. Support for XPath and System.Xml.XPath
      7. LINQ to XML Security
      8. LINQ to XML Serialization
      9. Summary
  10. IV. Advanced LINQ
    1. 11. Inside Expression Trees
      1. Lambda Expressions
      2. What Is an Expression Tree
        1. Creating Expression Trees
        2. Encapsulation
        3. Immutability and Modification
      3. Dissecting Expression Trees
        1. The Expression Class
        2. Expression Tree Node Types
          1. ExpressionType Enumeration
          2. Classes Derived from Expression
        3. Practical Nodes Guide
          1. LambdaExpression and ParameterExpression
          2. BinaryExpression
          3. ConstantExpression
          4. MethodCallExpression
          5. ConditionalExpression
          6. InvocationExpression
      4. Visiting an Expression Tree
      5. Dynamically Building an Expression Tree
        1. How the Compiler Generates an Expression Tree
        2. Combining Existing Expression Trees
        3. Dynamic Composition of an Expression Tree
      6. Summary
    2. 12. Extending LINQ
      1. Custom Operators
      2. Specialization of Existing Operators
        1. Dangerous Practices
        2. Limits of Specialization
      3. Creating a Custom LINQ Provider
        1. The IQueryable Interface
        2. From IEnumerable to IQueryable and Back
        3. Inside IQueryable and IQueryProvider
        4. Writing the FlightQueryProvider
          1. The FlightStatusService class
          2. Implementing IQueryable in FlightQuery
          3. Implementing IQueryProvider in FlightQueryProvider
          4. Implementing an ExpressionVisitor in FlightQueryTranslator
          5. Working with the FlightQueryProvider
          6. Cost-Benefit Balance of a Custom LINQ Provider
      4. Summary
    3. 13. Parallel LINQ
      1. Parallel Extensions to the .NET Framework
        1. Parallel.For and Parallel.ForEach Methods
        2. Do Method
        3. Task Class
        4. Future<T> Class
        5. Concurrency Considerations
      2. Using PLINQ
        1. Threads Used by PLINQ
        2. PLINQ Implementation
        3. PLINQ Use
          1. Pipelined Processing
          2. Stop-and-Go Processing
          3. Inverted Enumeration
        4. Side Effects of Parallel Execution
          1. Order of Results
          2. Changes in Data During Execution
          3. Processing Query Results
        5. Exception Handling with PLINQ
        6. PLINQ and Other LINQ Implementations
      3. Summary
    4. 14. Other LINQ Implementations
      1. Database Access
      2. Data Access Without a Database
      3. LINQ to Entity Domain Models
      4. LINQ to Services
      5. LINQ for System Engineers
      6. Dynamic LINQ
      7. Other LINQ Enhancements and Tools
      8. Summary
  11. V. Applied LINQ
    1. 15. LINQ in a Multitier Solution
      1. Characteristics of a Multitier Solution
      2. LINQ to SQL in a Two-Tier Solution
      3. LINQ in an n-Tier Solution
        1. LINQ to SQL as a DAL Replacement
        2. Abstracting LINQ to SQL with XML External Mapping
        3. Using LINQ to SQL Through Real Abstraction
        4. LINQ to XML as the Data Layer
        5. LINQ to Entities as the Data Layer
      4. LINQ in the Business Layer
        1. LINQ to Objects to Write Better Code
        2. IQueryable<T> versus IEnumerable<T>
        3. Identifying the Right Unit of Work
        4. Handling Transactions
        5. Concurrency and Thread Safety
      5. Summary
    2. 16. LINQ and ASP.NET
      1. ASP.NET 3.5
        1. ListView
        2. ListView Data Binding
        3. DataPager
      2. LinqDataSource
        1. Paging Data with LinqDataSource and DataPager
        2. Handling Data Modifications with LinqDataSource
        3. Using Custom Selections with LinqDataSource
        4. Using LinqDataSource with Custom Types
      3. Binding to LINQ queries
      4. Summary
    3. 17. LINQ and WPF/Silverlight
      1. Using LINQ with WPF
        1. Binding Single Entities and Properties
        2. Binding Collections of Entities
      2. Using LINQ with Silverlight
      3. Summary
    4. 18. LINQ and the Windows Communication Foundation
      1. WCF Overview
        1. WCF Contracts and Services
        2. Service Oriented Contracts
        3. Endpoint and Service Hosting
        4. Service Consumers
      2. WCF and LINQ to SQL
        1. LINQ to SQL Entities and Serialization
        2. Publishing LINQ to SQL Entities with WCF
        3. Consuming LINQ to SQL Entities with WCF
      3. LINQ to Entities and WCF
      4. Query Expression Serialization
      5. Summary
  12. VI. Appendixes
    1. A. ADO.NET Entity Framework
      1. ADO.NET Standard Approach
      2. Abstracting from the Physical Layer
      3. Entity Data Modeling
        1. Entity Data Model Files
        2. Entity Data Model Designer and Wizard
        3. Entity Data Model Generation Tool
        4. Entity Data Model Rules and Definition
      4. Querying Entities with ADO.NET
      5. Querying ADO.NET Entities with LINQ
      6. Managing Data with Object Services
        1. Object Identity Management
        2. Transactional Operations
      7. Manually Implemented Entities
      8. LINQ to SQL and ADO.NET Entity Framework
      9. Summary
    2. B. C# 3.0: New Language Features
      1. C# 2.0 Revisited
        1. Generics
        2. Delegates
        3. Anonymous Methods
        4. Enumerators and Yield
      2. C# 3.0 Features
        1. Automatically Implemented Properties
        2. Local Type Inference
        3. Lambda Expressions
        4. Extension Methods
        5. Object Initialization Expressions
        6. Anonymous Types
          1. Anonymous Types Are Immutable
        7. Query Expressions
        8. Partial Methods
      3. Summary
    3. C. Visual Basic 2008: New Language Features
      1. Visual Basic 2008 and Nullable Types
      2. The If Operator
      3. Visual Basic 2008 Features Corresponding to C# 3.0
        1. Local Type Inference
        2. Extension Methods
        3. Object Initialization Expressions
        4. Anonymous Types
          1. Anonymous Types Are Not Immutable in Visual Basic 2008
        5. Query Expressions
        6. Lambda Expressions
        7. Closures
        8. Partial Methods
      4. Visual Basic 2008 Features Without C# 3.0 Counterparts
        1. XML Support
          1. XML Literals
          2. Late Binding over XML
        2. Relaxed Delegates
      5. C# 3.0 Features Without Visual Basic 2008 Counterparts
        1. The yield Keyword
        2. Anonymous Methods
      6. Summary
  13. About the Authors
  14. Copyright