Contents at a Glance
Foreword
Acknowledgments
About the Authors
1. Introduction
2. Getting Started
3. The Essence of LINQ
4. C# 3.0 Technical Overview
5. Writing Query Expressions
6. Query Operators
7. A Quick Tour of LINQ to SQL
8. Reading Objects with LINQ to SQL
9. Modifying Objects with LINQ to SQL
10. Using Stored Procedures and Database Functions with LINQ to SQL
11. Customizing Entity Persistence and Adding Business Logic
12. LINQ to Entities Overview
13. LINQ to XML: Creation
14. Querying and Editing XML
15. XML Namespaces, Transformations, and Schema Validation
16. Introduction to LINQ Patterns and Practices
17. LINQ Everywhere
18. Conclusion
A. Tips for Developers
Index
Contents
Foreword
Acknowledgments
About the Authors
1. Introduction
The Varied Uses of the LINQ Syntax
LINQ Is a Practical Technology
Audience and Subject Matter
The Essence of LINQ
Comparing LINQ to SQL and LINQ to Objects
A Few Words About Generics
Source for the Samples and Troubleshooting Resources
Summary
2. Getting Started
Querying a Collection of Integers
Collection Initializers
Query Expressions
Type Inference
Introduction to IEnumerable<T>
Querying a Collection of Objects
Introducing Automatic Properties
Introducing Object Initializers
Introducing Anonymous Types
A Simple LINQ to SQL Example
LINQ to XML
Parsing XML
Creating XML
Summary
3. The Essence of LINQ
Integrated
Unitive
Extensible Provider Model
Query Operators
Declarative: Not How, But What
Hierarchical
Composable
Transformative
Summary
4. C# 3.0 Technical Overview
C# 2.0 and 3.0 Features Related to LINQ
Partial Methods
Automatically Implemented Properties
Initializers
Collection Initializers
Object Initializers
Types in C# 3.0
Type Inference
Anonymous Types
Generic Methods, Delegates, and Lambdas
Delegates
Generic Delegates
Lambdas
Extension Methods
Scoping Issues
IEnumerable<T>
Understanding Sequences
Enumeration
Iterators
Deferred Execution
Overriding LINQ Operators
Expression Trees
Summary
5. Writing Query Expressions
Syntactical Analysis
Nomenclature
Clauses
Range Variables
Composing Queries
Group-by Clauses at the End of a Query Expression
Group-by Clauses and the into Keyword
Let Clauses
Joins
Inner Joins
Group Joins
Left Outer Joins
Using the Object Model to “Join” Classes
Projections
Overview of Projections
Projections and Deferred Execution
Projections with SelectMany
The SelectMany Overloads
Query Expressions and Other Flavors of LINQ
LINQ to MyNumberServer
Thinking About IQueryable
Summary
6. Query Operators
Locating and Grouping the LINQ Operators
Code Reuse
Locating the LINQ Operators
Generation Operators
Range
Repeat
Empty
Any
All
Contains
SequenceEqual
Partitioning Operators
Take
Skip
TakeWhile
SkipWhile
Element Operators
First and FirstOrDefault
Last and LastOrDefault
Single
ElementAt
Element Operators and Composition
DefaultIfEmpty
Set Operators
Union
Intersect
Distinct
Except
In the Context of LINQ
Aggregate Operators
The Count and LongCount Operators
The Min and Max Operators
The Average Operator
The Sum Operator
The Aggregate Operator
Ordering Operators
OrderBy
OrderByDescending
ThenBy
Reverse
Conversion Operators
ToList
ToArray
OfType
ToDictionary
Conversion Between IEnumerable and IQueryable
Summary
7. A Quick Tour of LINQ to SQL
Mapping Classes to Tables
Creating Entity Classes
The DataContext
Working with Relationships
Defining Relationships
Querying Across Relationships
Modifying and Saving Entities
Using the Graphical Designer for Mapping
Using the Command-Line Tool for Mapping
Summary
8. Reading Objects with LINQ to SQL
Using LINQ and Databases
Translating LINQ to SQL
Understanding the Nuances of Translation
Retrieving Objects: Entities and Projections
The Importance of Object Identity
Using Relationships
Joining Tables
Mapping Different Types of Relationships
Loading Options
Deferred Loading
Eager Loading
Defining Inheritance
Performance and Security
Query Versus Results
Compiled Queries
Security
Summary
9. Modifying Objects with LINQ to SQL
Entity Lifecycle
Inserting and Deleting Entities
Updating Entities
Automatically Maintained Relationships
Submitting Changes
Simultaneous Changes
Optimistic Concurrency
Refreshing Entities
Transactions and Connection Management
Attaching Multitier Entities
Data Binding
Smart Client Data Binding
ASP.NET Data Binding
Creating a Database
Summary
10. Using Stored Procedures and Database Functions with LINQ to SQL
Stored Procedures and Functions for Querying
Mapping and Using a Stored Procedure
Using Table-Valued Functions
Using Scalar-Valued Functions
Stored Procedures for Inserts, Updates, and Deletes
Stored Procedures for Loading Relationships
Summary
11. Customizing Entity Persistence and Adding Business Logic
Customizing Generated Code
Customizing the DataContext Class
Customizing the Entity Classes
Writing Your Own Persistent Classes
Summary
12. LINQ to Entities Overview
Understanding Entity Framework Concepts and Components
The Entity Data Model
Entity Framework Components
Using the Entity Framework
Entity Model Generation
Understanding the Generated Code
Performing CRUD Operations
Using Stored Procedures
Making Sense of LINQ to Relational Choices
Summary
13. LINQ to XML: Creation
XML Fundamentals
Understanding the LINQ to XML API
Creating XML Elements
Creating XML Attributes
Creating an XML Document
Creating an XML Declaration
Creating a Document from Raw Text
Building a Document One Node at a Time
Reading and Writing XML
Summary
14. Querying and Editing XML
More on XDocument, XElement, and XAttribute
Querying with Element and Elements
XML Descendants
Composition and XML Queries
DescendantNodes, XText, and CData
DescendantNodesAndSelf
Searching for Text Nodes
CData
Parents and Ancestors
Elements After or Before Self
Working with Missing Nodes
Working with Line Numbers
Modifying XML
Removing Nodes
Editing Nodes
Inserting Nodes
Summary
15. XML Namespaces, Transformations, and Schema Validation
XML and Namespaces
Default Namespaces
XML Transformations
Transforming XML from One Format to Another
Creating XHTML Through a Transformation
Transforming Relational Data into XML
Programmatically Creating the Database
Transferring Data from an XML File to a Database
Viewing the Data Schemas
XML Schema Validation
Validation
Namespaces and Validation
Annotations
Should You Use C# or VB?
Summary
16. Introduction to LINQ Patterns and Practices
Using Language Features Judiciously
Going Beyond Stored Procedures: The Dynamic SQL Debate
Designing Mid-tier with Persistent Entities and Business Logic
Data Shaping
Separation of Concerns
Managing Concurrency
Limitations of Optimistic Concurrency Checks
Unit of Work and Reusing a DataContext Instance
Understanding Performance
Defining Context
Costs and Optimizations
Improving Security
Summary
17. LINQ Everywhere
Other Flavors of LINQ
Parallel LINQ
Query Data with Parallel LINQ
LINQ to Flicker
LINQ to SharePoint
Working with Processes
Summary
18. Conclusion
A. Tips for Developers
Accessing the Source Code
Northwind and the Visual Studio Samples
Version Numbers
Essential Downloads
Installing Visual Studio Express
Installing the .NET Framework
Installing SQL Server Express
Compiling C# Programs
Compiling from the Command Line
Connecting to a Database
Using the Object Relational Designer
Summary
Important Resources
Reference Materials: Getting Help
Finding LINQ Providers
Including Data Files in Your Project
C# Keywords and Contextual Keywords
Visual C# 2008 Key Bindings
Answers to Chapter 4 Exercises
Index