Table of Contents
Pt. 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
Pt. 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
Pt. 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
Pt. 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 499
Forewords & Introductions
IntroductionIntroduction
By the time you are holding this book in your hands, I will have 30 years in since the first time I wrote some code. That code was ROM-BASIC on a TRS-80 in Washington grammar school in Owosso, Michigan, and I was in the fifth grade. Making the "tank" slide back and forth shooting blips across the screen was neat. Changing the code to change blip speeds and numbers of targets was exhilarating. Three decades later and I get more excited each passing year. There are great technologies on the horizon like Microsoft Surface, Popfly, and LINQ. This book is about LINQ, or Language INtegrated Query.
LINQ is a SQL-like language for C#. When I first saw it, I didn't like it. My first impression was that someone had glommed on a bastardization of C# and it was ugly like SQL can get. I didn't like it because I didn't understand it. However, I gave LINQ a second chance (as I want you to do) and discovered that LINQ is thoroughly integrated, tremendously powerful, and almost as much fun as a Tesla Roadster or doing hammerheads in an Extra 300L.
The query capabilities of LINQ are extended to objects, SQL, DataSets, XML, XSD, entities, and can be extended to other providers like Active Directory or SharePoint. This means that you can write queriesthat are similar in syntaxagainst objects, data, XML, XSD, entities, or Active Directory (with a little work) much like you would a SQL query in a database. And, LINQ is actually engineered artfully and brilliantly on top of generics as well as some new features in.NET 3.5, such as extension methods, anonymous types, and Lambda Expressions. Another very importantcharacteristic of LINQ is that it clearly demonstrates Microsoft's willingness to innovate and take the best of existing technologies like Lambda Calculusinvented in the 1930sand if it's good or great, incorporate these elements into the tools and languages we love.
LINQ and its underpinnings are powerful and challenging, and in this book you will get what you need to know to completely understand all that makes LINQ work and begin using it immediately. You will learn about anonymous methods, extension methods, Lambda Expressions, state machines, how generics and the CodeDOM play a big role in powerful tools like LINQ, and writing LINQ queries and why you will want to do it in the bigger, grander scheme of things. You will also learn how to save a ton of time and effort by not hard-coding those elements that you will no longer need or want to hard-code, and you will have a better grasp of how LINQ fits into n-tier architectures without breaking guidelines that have helped you succeed to date.
Brought to you by a four-time Microsoft MVP and columnist for over a decade, LINQ Unleashed for C# will teach you everything you need to know about LINQ and.NET 3.5 features and how to be more productive and have more fun than ever before.
Conventions Used in This BookThe following typographic conventions are used in this book:
Code lines, commands, statements, variables, and text you see onscreen appear in a monospace typeface.
Occasionally in listings bold is used to draw attention to the snippet of code being discussed.
Placeholders in syntax descriptions appear in an italic monospace typeface. You replace the placeholder with the actual filename, parameter, or whatever element it represents.
Italics highlight technical terms when they're being defined.
A code-continuation icon is used before a line of code that is really a continuation of the preceding line. Sometimes a line of code is too long to fit as a single line on the page. If you see before a line of code, remember that it's part of the line immediately above it.
The book also contains Notes, Tips, and Cautions to help you spot important or useful information more quickly.
© Copyright Pearson Education. All rights reserved.