Foreword by Darryl Hogan, Architect Evangelist, Microsoft Corporation Microsoft's highly anticipated LINQ query technology makes it easy to retrieve any information programmatically from any data source, no matter where it comes from or how it's stored. Using LINQ, developers can query objects, relational databases, XML documents, and ADO.NET datasets--and do it all directly from C# 3.0, leveraging the powerful capabilities of LINQ. This is a definitive guide to getting real-world results with LINQ, using C# 3.0 and Visual Studio 2008. In LINQ Unleashed , Microsoft MVP Paul Kimmel covers every facet of LINQ programming, showing how LINQ can help you dramatically improve your productivity and build more reliable, maintainable applications. Kimmel begins by reviewing the state-of-the-art C# programming techniques LINQ uses, including anonymous types, partial methods, and Lambda expressions. Next, using realistic examples and easy-to-adapt sample code, he details the most powerful new LINQ techniques for accessing objects, databases, and XML. You'll gain a deep and practical understanding of how LINQ works "under the hood"--and learn how to do everything from selecting data through integrating XML with other data models. Build efficient LINQ queries to .NET objects, SQL databases, and XML content Utilize anonymous types to reduce design time, coding effort, and debugging time Automatically generate .NET state machines with the new yield return construct Master LINQ query syntax, operators, extension methods, sorting, grouping, aggregate and set operations, and more Make the most of select--and use it in the business layer of your n-tier applications Query relational data stored in Microsoft SQL Server Use nullable types to eliminate unnecessary database access plumbing code Use LINQ with ADO.NET 3.0 and Microsoft's powerful new Entity Framework Extract XML data without the hassles or complexity of XPath Automatically construct XML from CSV files and other non-XML data Query Active Directory by extending LINQ Introduction 1 Part I Getting Ready for LINQ 1 Programming with Anonymous Types 5 2 Using Compound Type Initialization 29 3 Defining Extension and Partial Methods 61 4 yield return Using .NET's State Machine Generator 85 5 Understanding Lambda Expressions and Closures 97 6 Using Standard Query Operators 121 Part II LINQ for Objects 7 Sorting and Grouping Queries 137 8 Using Aggregate Operations 151 9 Performing Set Operations 167 10 Mastering Select and SelectMany 185 11 Joining Query Results 211 12 Querying Outlook and Active Directory 239 Part III LINQ for Data 13 Querying Relational Data with LINQ 265 14 Creating Better Entities and Mapping Inheritance and Aggregation 289 15 Joining Database Tables with LINQ Queries 309 16 Updating Anonymous Relational Data 349 17 Introducing ADO.NET 3.0 and the Entity Framework 383 Part IV LINQ for XML 18 Extracting Data from XML 415 19 Comparing LINQ to XML with Other XML Technologies 437 20 Constructing XML from Non-XML Data 453 21 Emitting XML with the XmlWriter 463 22 Combining XML with Other Data Models 469 23 LINQ to XSD Supports Typed XML Programming 485 Index Cover......Page 1 Contents......Page 5 Introduction......Page 18 Conventions Used in This Book......Page 19 Part I: Getting Ready for LINQ......Page 20 1 Programming with Anonymous Types......Page 22 Understanding Anonymous Types......Page 23 Programming with Anonymous Types......Page 24 Databinding Anonymous Types......Page 35 Testing Anonymous Type Equality......Page 40 Using Anonymous Types with LINQ Queries......Page 41 Introducing Generic Anonymous Methods......Page 42 Summary......Page 45 2 Using Compound Type Initialization......Page 46 Initializing Objects with Named Types......Page 47 Initializing Anonymous Types......Page 51 Initializing Collections......Page 53 Using Conversion Operators......Page 68 Summary......Page 77 Extension Methods and Rules of the Road......Page 78 Defining Extension Methods......Page 81 How Extension Methods Support LINQ......Page 90 Implementing a "Talking" String Extension Method......Page 95 Defining Partial Methods......Page 96 Summary......Page 101 4 yield return: Using .NET's State Machine Generator......Page 102 Understanding How yield return Works......Page 103 Using yield return and yield break......Page 105 Summary......Page 112 5 Understanding Lambda Expressions and Closures......Page 114 Understanding the Evolution from Function Pointers to Lambda Expressions......Page 115 Writing Basic Lambda Expressions......Page 118 Dynamic Programming with Lambda Expressions......Page 127 Lambda Expressions and Closures......Page 134 Currying......Page 136 Summary......Page 137 Understanding How LINQ Is Implemented......Page 138 Filtering Information......Page 139 Using Quantifiers......Page 141 Partitioning with Skip and Take......Page 143 Using Generation Operations......Page 144 Equality Testing......Page 146 Obtaining Specific Elements from a Sequence......Page 148 Appending Sequences with Concat......Page 149 Summary......Page 150 Part II: LINQ for Objects......Page 152 Sorting Information......Page 154 Grouping Information......Page 162 Summary......Page 167 Aggregating......Page 168 Averaging Collection Values......Page 171 Finding Minimum and Maximum Elements......Page 174 Summing Query Results......Page 179 Median: Defining a Custom Aggregation Operation......Page 180 Summary......Page 182 Finding Distinct Elements......Page 184 Defining Exclusive Sets with Intersect and Except......Page 194 Creating Composite Resultsets with Union......Page 199 Summary......Page 201 Exploring Select......Page 202 Projecting New Types from Multiple Sources......Page 220 Creating a New Sequence from Multiple Sequences with SelectMany......Page 222 Using SelectMany with Indexes......Page 224 Summary......Page 226 Using Multiple From Clauses......Page 228 Defining Inner Joins......Page 230 Using Custom, or Nonequijoins......Page 231 Implementing Group Join and Left Outer Join......Page 241 Implementing a Cross Join......Page 245 Summary......Page 254 LINQ to Outlook......Page 256 Querying Active Directory with Straight C# Code......Page 260 LINQ to Active Directory......Page 262 Querying Active Directory with LINQ......Page 277 Summary......Page 279 Part III: LINQ for Data......Page 280 13 Querying Relational Data with LINQ......Page 282 Defining Table Objects......Page 283 Connecting to Relational Data with DataContext Objects......Page 292 Querying DataSets......Page 294 Using the LINQ to SQL Class Designer......Page 302 Summary......Page 304 Defining Better Entities with Nullable Types......Page 306 Mapping Inheritance Hierarchies for LINQ to SQL......Page 311 Adding EntitySet Classes as Properties......Page 317 Creating Databases with LINQ to SQL......Page 322 Summary......Page 325 15 Joining Database Tables with LINQ Queries......Page 326 Defining Joins with LINQ to DataSet......Page 327 Defining Joins with LINQ to SQL......Page 334 Querying Views with LINQ......Page 357 Databinding with LINQ to SQL......Page 362 Summary......Page 364 Adding and Removing Data......Page 366 Calling User-Defined Functions......Page 380 Using Transactions......Page 383 Understanding Conflict Resolution......Page 385 N-Tier Applications and LINQ to SQL......Page 393 Summary......Page 399 17 Introducing ADO.NET 3.0 and the Entity Framework......Page 400 Understanding the General Nature of the Problem and the Solution......Page 401 Finding Additional Resources......Page 403 Building a Sample Application Using Vanilla ADO.NET Programming......Page 406 Programming with the Entity Framework......Page 418 Doing It All with LINQ......Page 423 Summary......Page 428 Part IV: LINQ for XML......Page 430 Loading XML Documents......Page 432 Querying XML Documents......Page 433 Loading XML from a String......Page 441 Handling Missing Data......Page 442 Using Query Expressions with XML Data......Page 443 Annotating Nodes......Page 450 Summary......Page 452 19 Comparing LINQ to XML with Other XML Technologies......Page 454 Comparing LINQ to XML with XPath......Page 455 Comparing LINQ to XML Transformations with XSLT......Page 460 Transforming XML Data Using Functional Construction......Page 467 Summary......Page 469 20 Constructing XML from Non-XML Data......Page 470 Constructing XML from CSV Files......Page 471 Generating Text Files from XML......Page 473 Using XML and Embedded LINQ Expressions (in VB)......Page 475 Summary......Page 479 21 Emitting XML with the XmlWriter......Page 480 Exploring the XmlWriter, Quickly......Page 481 Using XmlTextWriter to Write an XML File......Page 482 Summary......Page 484 Creating XML from SQL Data......Page 486 Updating SQL Data from XML......Page 495 Summary......Page 500 23 LINQ to XSD Supports Typed XML Programming......Page 502 Understanding the Basic Design Goals of LINQ to XSD......Page 503 Programming with LINQ to XSD......Page 504 Summary......Page 515 A......Page 516 C......Page 518 D......Page 521 E......Page 523 F......Page 525 I......Page 526 J......Page 527 K–L......Page 528 M......Page 533 N......Page 535 P......Page 536 Q......Page 537 R......Page 538 S......Page 539 U......Page 541 W......Page 542 X–Z......Page 543
Foreword by Darryl Hogan, Architect Evangelist, Microsoft Corporation
Microsoft’s highly anticipated LINQ query technology makes it easy to retrieve any information programmatically from any data source, no matter where it comes from or how it’s stored. Using LINQ, developers can query objects, relational databases,
This is a definitive guide to getting real-world results with LINQ, using C# 3.0 and Visual Studio 2008. In LINQ Unleashed, Microsoft MVP Paul Kimmel covers every facet of LINQ programming, showing how LINQ can help you dramatically improve your productivity and build more reliable, maintainable applications.
Kimmel begins by reviewing the state-of-the-art C# programming techniques LINQ uses, including anonymous types, partial methods, and Lambda expressions. Next, using realistic examples and easy-to-adapt sample code, he details the most powerful new LINQ techniques for accessing objects, databases, and
- Build efficient LINQ queries to .NET objects, SQL databases, and
Utilize anonymous types to reduce design time, coding effort, and debugging time
- Automatically generate .NET state machines with the new yield return construct
- Master LINQ query syntax, operators, extension methods, sorting, grouping, aggregate and set operations, and more
- Make the most of select—and use it in the business layer of your n-tier applications
- Query relational data stored in Microsoft SQL Server
- Use nullable types to eliminate unnecessary database access plumbing code
- Use LINQ with ADO.NET 3.0 and Microsoft’s powerful new Entity Framework
- Extract
Automatically construct
Query Active Directory by extendingLINQ
Introduction 1
Part I Getting Ready for LINQ
1 Programming with Anonymous Types 5
2 Using Compound Type Initialization 29
3 Defining Extension and Partial Methods 61
4 yield return: Using .NET’s State Machine Generator 85
5 Understanding Lambda Expressions and Closures 97
6 Using Standard Query Operators 121
Part II LINQ for Objects
7 Sorting and Grouping Queries 137
8 Using Aggregate Operations 151
9 Performing Set Operations 167
10 Mastering Select and SelectMany 185
11 Joining Query Results 211
12 Querying Outlook and Active Directory 239
Part III LINQ for Data
13 Querying Relational Data with LINQ 265
14 Creating Better Entities and Mapping Inheritance and Aggregation 289
15 Joining Database Tables with LINQ Queries 309
16 Updating Anonymous Relational Data 349
17 Introducing ADO.NET 3.0 and the Entity Framework 383
Part IV LINQ for
18 Extracting Data from
19 Comparing LINQ to
20 Constructing
21 Emitting
463
22 Combining
23 LINQ to XSD Supports Typed
Index
This is the eBook version of the printed book. If the print book includes a CD-ROM, this content is not included within the eBook version.Foreword by Darryl Hogan, Architect Evangelist, Microsoft Corporation Microsoft's highly anticipated LINQ query technology makes it easy to retrieve any information programmatically from any data source, no matter where it comes from or how it's stored. Using LINQ, developers can query objects, relational databases, XML documents, and ADO.NET datasets--and do it all directly from C# 3.0, leveraging the powerful capabilities of LINQ. This is a definitive guide to getting real-world results with LINQ, using C# 3.0 and Visual Studio 2008. In LINQ Unleashed, Microsoft MVP Paul Kimmel covers every facet of LINQ programming, showing how LINQ can help you dramatically improve your productivity and build more reliable, maintainable applications. Kimmel begins by reviewing the state-of-the-art C# programming techniques LINQ uses, including anonymous types, partial methods, and Lambda expressions. Next, using realistic examples and easy-to-adapt sample code, he details the most powerful new LINQ techniques for accessing objects, databases, and XML. You'll gain a deep and practical understanding of how LINQ works “under the hood”--and learn how to do everything from selecting data through integrating XML with other data models. Build efficient LINQ queries to.NET objects, SQL databases, and XML content Utilize anonymous types to reduce design time, coding effort, and debugging time Automatically generate.NET state machines with the new yield return construct Master LINQ query syntax, operators, extension methods, sorting, grouping, aggregate and set operations, and more Make the most of select--and use it in the business layer of your n-tier applications Query relational data stored in Microsoft SQL Server Use nullable types to eliminate unnecessary database access plumbing code Use LINQ with ADO.NET 3.0 and Microsoft's powerful new Entity Framework Extract XML data without the hassles or complexity of XPath Automatically construct XML from CSV files and other non-XML data Query Active Directory by extending LINQ Introduction 1 Part I Getting Ready for LINQ 1 Programming with Anonymous Types 5 2 Using Compound Type Initialization 29 3 Defining Extension and Partial Methods 61 4 yield return: Using.NET's State Machine Generator 85 5 Understanding Lambda Expressions and Closures 97 6 Using Standard Query Operators 121 Part II LINQ for Objects 7 Sorting and Grouping Queries 137 8 Using Aggregate Operations 151 9 Performing Set Operatio "This is a definitive guide to getting real-world results with LINQ, using C# 3.0 and Visual Studio 2008. In LINQ unleashed, Microsoft MVP Paul Kimmel covers every facet of LINQ programming, showing how LINQ can help you dramatically improve your productivity and build more reliable, maintainable applications." "Kimmel begins by reviewing the state-of-the-art C# programming techniques LINQ uses, including anonymous types, partial methods, and Lambda expressions. Next, using realistic examples and easy-to-adapt sample code, he details the most powerful new LINQ techniques for accessing objects, databases, and XML. You'll gain a deep and practical understanding of how LINQ works "under the hood" - and learn how to do everything from selecting data through integrating XML with other data models."--book jacket A thorough overview of LINQ (Language Integrated Query), the most important new feature in Visual Studio 2008, explains how to use LINQ to improve access for .NET developers, furnishes helpful explanations that demonstrate how to use LINQ in applications, and covers such topics as conversion operators, lambda expressions and closures, relational data, and much more. Original. (Intermediate)