LINQ for Visual C# 2008 (Apress).pdf

(2769 KB) Pobierz
25149411 UNPDF
Available as a
PDF Electronic Book
or Print On Demand
Visual C# 2008
200
PAgeS
Fabio Claudio Ferracchiati
LINQ for
25149411.019.png 25149411.020.png 25149411.021.png 25149411.022.png 25149411.001.png 25149411.002.png 25149411.003.png 25149411.004.png 25149411.005.png 25149411.006.png 25149411.007.png 25149411.008.png 25149411.009.png 25149411.010.png 25149411.011.png 25149411.012.png 25149411.013.png 25149411.014.png 25149411.015.png
About firstPress
Apress's firstPress series is your source for understanding cutting-edge technology. Short,
highly focused, and written by experts, Apress's firstPress books save you time and effort. They
contain the information you could get based on intensive research yourself or if you were to
attend a conference every other week—if only you had the time. They cover the concepts and
techniques that will keep you ahead of the technology curve. Apress's firstPress books are real
books, in your choice of electronic or print-on-demand format, with no rough edges even when
the technology itself is still rough. You can't afford to be without them.
LINQ for Visual C# 2008
Dear Reader,
C # programmers at any level need to learn about LINQ (Language-Integrated Query),
Microsoft’s breakthrough technology for simplifying and unifying data access from any data
source. With LINQ you can write more elegant and flexible code, not just to access databases
and files but also to manipulate data structures and XML.
LINQ for Visual C# 2008 is a short guide to the major features of LINQ. It thoroughly covers
LINQ to Objects, LINQ to SQL, LINQ to DataSet, and LINQ to XML. For instance, you’ll
learn to
Use the LINQ syntax
Use LINQ to Objects to query in-memory objects
Integrate LINQ to SQL with existing ADO.NET programs
Query XML documents/data using LINQ to XML
Integrate LINQ to SQL and LINQ to XML
The book also includes plenty of working examples to demonstrate LINQ in action. There is no
better source than this book for getting a fast head start on this technology.
Best Regards,
Fabio Claudio Ferracchiati
Contents
Chapter 1: LINQ to Objects ......................................................... 2
Introduction............................................................................................. 2
A Simple C# 3.0 LINQ to Objects Program ........................................... 2
Extension Methods.................................................................................. 4
Lambda Expressions ............................................................................... 6
Expression Trees ..................................................................................... 7
Object Initialization Expressions ........................................................... 8
Anonymous Types ................................................................................... 9
Implicitly Typed Local Variables.......................................................... 10
Query Evaluation Time......................................................................... 11
Standard Query Operators.................................................................... 15
Restriction Operator .................................................................................... 22
Projection Operators.................................................................................... 25
Join Operators.............................................................................................. 29
Grouping Operator ...................................................................................... 33
Ordering Operators...................................................................................... 38
Aggregate Operators.................................................................................... 43
Partitioning Operators ................................................................................. 51
Concatenation Operator............................................................................... 54
Element Operators ....................................................................................... 55
Generation Operators .................................................................................. 61
Quantifier Operators.................................................................................... 63
Equality Operator ........................................................................................ 65
LINQ for Visual C# 2008
i
25149411.016.png
Set Operators ............................................................................................... 66
Conversion Operators.................................................................................. 69
Summary................................................................................................ 75
Chapter 2: LINQ to ADO.NET..................................................... 76
Introduction........................................................................................... 76
Database Interaction............................................................................. 77
Mapping a Class to a Database Table ......................................................... 77
Mapping Fields and Properties to Table Columns ...................................... 78
Creating a Data Context .............................................................................. 83
Querying a Database with LINQ to SQL .................................................... 85
Adding, Modifying, and Deleting Rows ..................................................... 89
DataContext: Advanced Features ........................................................ 92
Defining Relationships Between Entities.................................................... 92
Using Two Related Entity Classes ............................................................ 101
Other LINQ to SQL Features ............................................................. 105
SQLMetal .................................................................................................. 105
The INotifyPropertyChanging Interface ................................................... 108
Optimistic Concurrency and Database Transactions ................................ 110
Stored Procedures...................................................................................... 116
User-Defined Functions ............................................................................ 123
Database Creation...................................................................................... 125
LINQ to SQL in Visual Studio 2008 .................................................. 127
A Linq to SQL File Designer Example ..................................................... 127
Debugging LINQ Applications ................................................................. 138
LINQ to DataSet.................................................................................. 145
Summary.............................................................................................. 149
Chapter 3: LINQ to XML .......................................................... 150
ii
LINQ for Visual C# 2008
25149411.017.png
Introduction......................................................................................... 150
Querying XML .................................................................................... 150
Searching for Attribute Values.................................................................. 154
The Descendants and Ancestors Methods................................................. 155
Querying XML for Content Type ............................................................. 156
Querying an XML Document That Uses Schemas ................................... 157
The ElementsBeforeSelf and ElementsAfterSelf Methods....................... 160
Miscellaneous Functionalities ................................................................... 161
Creating and Modifying XML Documents ........................................ 165
Creating an XML Document from Scratch ............................................... 165
Loading and Saving XML......................................................................... 171
Modifying XML ........................................................................................ 173
LINQ to XML and LINQ to SQL ............................................................. 180
Summary.............................................................................................. 184
Related Titles ......................................................................... 185
Copyright ............................................................................... 186
What Is LINQ? .................................................................................... 192
Why LINQ? ......................................................................................... 192
What You Need to Use LINQ ............................................................. 195
Resources............................................................................................. 195
What’s Next? ....................................................................................... 196
LINQ for Visual C# 2008
iii
25149411.018.png
Zgłoś jeśli naruszono regulamin