List of Listings

Chapter 1. Introducing LINQ

Listing 1.1. Sample code that uses LINQ to query a database and create an XML document

Listing 1.2. Typical .NET data-access code

Listing 1.3. Simple query expression

Listing 1.4. NHibernate mapping file used to map a Cat class to a CATS table in a relational database

Listing 1.5. Working with relational data and XML in the same query

Listing 1.6. Hello LINQ in C# (HelloLinq.csproj)

Listing 1.7. Hello LINQ in VB.NET (HelloLinq.vbproj)

Listing 1.8. Old-school version of Hello LINQ (OldSchoolHello.csproj)

Listing 1.9. Hello LINQ in C# improved with grouping and sorting (HelloLinqWithGroupingAndSorting.csproj)

Listing 1.10. Hello LINQ in VB improved with grouping and sorting (HelloLinqWithGroupingAndSorting.vbproj)

Listing 1.11. Hello LINQ to XML in C# (HelloLinqToXml.csproj)

Listing 1.12. Hello LINQ to XML in VB.NET (HelloLinqToXml.vbproj)

Listing 1.13. Old-school version of Hello LINQ to XML (OldSchoolXml.csproj)

Listing 1.14. Hello LINQ to XML VB.NET using XML literals (HelloLinqWithLiterals.vbproj)

Listing 1.15. Hello LINQ to SQL complete source code (HelloLinqToSql.csproj)

Chapter 2. C# and VB.NET language enhancements

Listing 2.1. Sample .NET 2.0 code for listing processes (DotNet2.csproj)

Listing 2.2. Improved .NET 2.0 code for listing processes (DotNet2Improved.csproj)

Listing 2.3. Our DisplayProcesses method using the var keyword (UsingVar.csproj)

Listing 2.4. DisplayProcesses method using a constructor for ProcessData

Listing 2.5. DisplayProcesses method using an object initializer (ObjectInitializer.csproj)

Listing 2.6. DisplayProcesses method with a hard-coded filtering condition

Listing 2.7. DisplayProcesses method that uses a delegate for filtering

Listing 2.8. Calling the DisplayProcesses method using a standard delegate

Listing 2.9. Calling the DisplayProcesses method using an anonymous method

Listing 2.10. Calling the DisplayProcesses method using a lambda expression (LambdaExpressions.csproj)

Listing 2.11. Sample lambda expressions in C#

Listing 2.12. Sample lambda expressions in VB.NET

Listing 2.13. Sample lambda expressions declared as delegates in C# (LambdaExpressions.csproj)

Listing 2.14. Sample lambda expressions declared as delegates in VB.NET (LambdaExpressions.vbproj)

Listing 2.15. The TotalMemory helper method coded as standard static method

Listing 2.16. The TotalMemory helper method declared as an extension method (ExtensionMethods.csproj)

Listing 2.17. Sample extension method in VB.NET (ExtensionMethods.vbproj)

Listing 2.18. The DisplayProcesses methods with extension methods (ExtensionMethods.csproj)

Listing 2.19. Sample code for demonstrating extension methods’ discoverability

Listing 2.20. The DisplayProcesses method with an anonymous type (AnonymousTypes.csproj)

Listing 2.21. Testing keyed anonymous types (AnonymousTypes.csproj)

Listing 2.22. Complete code demonstrating the new language features (CompleteCode.csproj)

Chapter 3. LINQ building blocks

Listing 3.1. Querying a list of processes using extension methods

Listing 3.2. The Where method that is used in our sample query

Listing 3.3. Sample iterator (Iterator.csproj)

Listing 3.4. Deferred query execution demonstration (DeferredQueryExecution.csproj)

Listing 3.5. Same query producing different results between two executions (QueryReuse.csproj)

Listing 3.6. A query expressed as static method calls

Listing 3.7. C# query expression that uses query operators (QueryExpressionWithOperators.csproj)

Listing 3.8. C# query that uses query operators only (QueryExpressionWithOperators.csproj)

Chapter 4. Getting familiar with LINQ to Objects

Listing 4.1. LinqBooks projectLinqBooks.CommonSampleData.csLinqBooks.CommonSampleData.csprojectsLinqBooks.CommonSampleData.csThe SampleData class provides sample data (LinqBooks.CommonSampleData.cs)

Listing 4.2. Querying an untyped array with LINQ to Objects (UntypedArray.csproj)

Listing 4.3. Querying a typed array with LINQ to Objects (TypedArray.csproj)

Listing 4.4. Querying a generic list with LINQ to Objects (GenericList.csproj)

Listing 4.5. Querying a generic dictionary with LINQ to Objects (GenericDictionary.csproj)

Listing 4.6. Querying a string with LINQ to Objects (String.csproj)

Listing 4.7. Markup for the first ASP.NET page (Step1.aspx)

Listing 4.8. Code-behind for the first ASP.NET page (Step1.aspx.cs)

Listing 4.9. Markup for a richer collection (Step2a.aspx)

Listing 4.10. Code-behind for a richer collection (Step2a.aspx.cs)

Listing 4.11. Code-behind for a richer collection using an anonymous type (Step2b.aspx.cs)

Listing 4.12. Markup for listing 4.11 (Step2b.aspx)

Listing 4.13. Code-behind for the first form (FormStrings.cs)

Listing 4.14. Sample use of the Select query operator with indices (SelectIndex.csproj)

Listing 4.15. Retrieving a list of authors without using the Distinct query operator (Distinct.csproj)

Listing 4.16. Retrieving a list of authors using the Distinct query operator (Distinct.csproj)

Listing 4.17. Retrieving a list of authors using the VB Distinct keyword (Distinct.vbproj)

Listing 4.18. Using an orderby clause to sort results (Sorting.aspx.cs)

Listing 4.19. Markup used to display the results of the sorting sample (Sorting.aspx)

Listing 4.20. Code-behind that demonstrates nested queries (Nested.aspx.cs)

Listing 4.21. Markup for the nested queries (Nested.aspx)

Listing 4.22. Grouping books by publisher using a group clause (Grouping.aspx.cs)

Listing 4.23. Using a join..into clause to group books by publisher (Joins.aspx.cs)

Listing 4.24. Using a join clause to group books by publisher (Joins.aspx.cs)

Listing 4.25. Using the Join operator to group books by publisher

Listing 4.26. Query used to perform a left outer join (Joins.aspx.cs)

Listing 4.27. Query used to perform a cross join (Joins.aspx.cs)

Listing 4.28. Code-behind for paging in a GridView control (Paging.aspx.cs)

Listing 4.29. Code-behind for demonstrating partitioning (Partitioning.aspx.cs)

Chapter 5. Beyond basic in-memory queries

Listing 5.1. Trying to query an ArrayList using LINQ to Objects directly fails

Listing 5.2. Querying an ArrayList is possible thanks to the Cast query operator

Listing 5.3. Querying an ArrayList is possible thanks to type declarations in query expressions

Listing 5.4. Grouping books by publisher and subject

Listing 5.5. Using the into keyword in a group by clause

Listing 5.6. Query that groups book titles, and not book objects, by publisher and subject

Listing 5.7. Using a local variable to make a query dynamic

Listing 5.8. Using a method parameter to make a query dynamic

Listing 5.9. Method that uses a parameter to enable custom sorting

Listing 5.10. Method that uses a parameter in a query expression to enable custom sorting

Listing 5.11. Method that uses a parameter to enable custom sorting in ascending or descending order

Listing 5.12. Switch statement used to choose between several custom sorts

Listing 5.13. Building a conditional query based on user input

Listing 5.14. Dynamic query refactored into a method

Listing 5.15. Invoking the ConditionalQuery method according to user input

Listing 5.16. Complete version of the ConditionalQuery method that tests for the provided criteria

Listing 5.17. Completely creating a query at run-time using an expression tree

Listing 5.18. Sample CSV document containing information about books

Listing 5.19. Querying information about books from a CSV file

Listing 5.20. Declarative approach for parsing a CSV file, with anonymous types

Listing 5.21. Declarative for parsing a CSV file, with existing types

Listing 5.22. Imperative approach for parsing a CSV file

Listing 5.23. Standard code used to execute and enumerate a LINQ query

Listing 5.24. ForEach query operator that executes a function over each element in a source sequence

Listing 5.25. Using the ForEach query operator using the method syntax

Listing 5.26. Using the ForEach query operator with a query expression

Listing 5.27. Using multiple statements in a ForEach call

Listing 5.28. Lines query operator that yields the text lines from a source StreamReader

Listing 5.29. Using the Lines query operator to use a streaming approach in CSV parsing

Listing 5.30. Code used to parse a CSV document

Listing 5.31. Using a foreach statement to find the book with the highest number of pages in a collection

Listing 5.32. Using sorting and First to find the book with the highest number of pages in a collection

Listing 5.33. Using a subquery to find the book with the highest number of pages in a collection

Listing 5.34. Using two separate queries to find the book with the highest number of pages in a collection

Listing 5.35. Creating a custom operator named MaxElement to find the object with the maximum value

Listing 5.36. Filtering a collection of books with a LINQ query

Listing 5.37. Filtering a collection of books with a foreach loop

Listing 5.38. Filtering a collection of books with a for loop

Listing 5.39. Filtering a collection of books with the List<T>.FindAll method

Listing 5.40. Grouping with a LINQ query

Listing 5.41. Grouping without LINQ

Chapter 6. Getting started with LINQ to SQL

Listing 6.1. Querying Subjects and Books with LINQ to Objects

Listing 6.2. Selecting the book title and price for books less than $30

Listing 6.3. Starting Book class definition

Listing 6.4. The full Book class with basic mapping

Listing 6.5. Fetch books using LINQ to SQL

Listing 6.6. Fetch the list of book titles

Listing 6.7. Project into an anonymous type

Listing 6.8. Adding data paging using composition

Listing 6.9. VB syntax for paging data

Listing 6.10. Filtering using a range

Listing 6.11. Using mapped CLR methods

Listing 6.12. Sorting with LINQ to SQL

Listing 6.13. Grouping and sorting

Listing 6.14. Including aggregates in the results

Listing 6.15. Using multiple aggregates

Listing 6.16. Joining Books and Subjects

Listing 6.17. Joining with the Join keyword

Listing 6.18. Approximating an outer join

Listing 6.19. Rewriting the original example using LINQ to SQL

Listing 6.20. Mapping the object associations

Listing 6.21. Iterating over object trees

Listing 6.22. Using Any to achieve an inner join on object trees

Listing 6.23. Filtering child objects using All

Listing 6.24. Running query using object hierarchies

Listing 6.25. Lazy loading child objects

Listing 6.26. Generated output when lazy loading the child elements

Listing 6.27. Using DataLoadOptions to optimize object loading

Listing 6.28. Updating values and committing them to the database

Listing 6.29. Adding and removing items from a table

Chapter 7. Peeking under the covers of LINQ to SQL

Listing 7.1. XML mapping file for Author class

Listing 7.2. Attaching the external XML mapping to the DataContext

Listing 7.3. Query expressed as expressions

Listing 7.4. Identity management and change tracking

Listing 7.5. Submitting changes with identity and change tracking management

Listing 7.6. Updating records in a disconnected environment

Listing 7.7. Updating a disconnected object that has already been changed

Chapter 8. Advanced LINQ to SQL features

Listing 8.1. SQL Update statement to perform optimistic concurrency on Book

Listing 8.2. Default concurrency implementation with LINQ to SQL

Listing 8.3. Optimistic concurrency with Authors using a timestamp column

Listing 8.4. Resolving change conflicts with KeepChanges

Listing 8.5. Replacing the user’s values with ones from the database

Listing 8.6. Displaying conflict details

Listing 8.7. Managing the transaction through the DataContext

Listing 8.8. Managing transactions with the TransactionScope object

Listing 8.9. Dynamic SQL pass-through

Listing 8.10. Dynamic SQL pass-through with parameters

Listing 8.11. Using a stored procedure to return results

Listing 8.12. Generated GetBook code to call the stored procedure

Listing 8.13. Returning a scalar value

Listing 8.14. Consuming a scalar stored procedure

Listing 8.15. Stored procedure to update an Author

Listing 8.16. Consuming the update stored procedure using LINQ

Listing 8.17. UpdateT(T instance) method to replace the run-time implementation

Listing 8.18. User-defined scalar function

Listing 8.19. LINQ code generated for the scalar function

Listing 8.20. Using a scalar user-defined function in a query

Listing 8.21. Defining and consuming a table-valued function

Listing 8.22. Consuming user-defined functions

Listing 8.23. Precompiling a query

Listing 8.24. Adding functionality with partial classes

Listing 8.25. Querying with a property from the partial class

Listing 8.26. Implementing IDataErrorInfo in the custom partial class

Listing 8.27. Partial signature of the generated class including partial methods

Listing 8.28. Consuming inherited LINQ to SQL objects

Chapter 9. Introducing LINQ to XML

Listing 9.1. Sample XML file containing web site links

Listing 9.2. The most important book in anyone’s library

Listing 9.3. Create an XML document using the DOM

Listing 9.4. Create an XML document using LINQ to XML

Listing 9.5. An RSS feed that uses XML namespaces

Listing 9.6. Working with XML containing namespaces via the DOM

Listing 9.7. Querying XML containing namespaces with LINQ to XML

Listing 9.8. Creating an XElement from an existing XmlReader

Listing 9.9. Creating an XElement object from a fragment of XML contained within an XmlReader

Listing 9.10. Parsing a string of XML to an XElement

Listing 9.11. Creating an XElement with functional construction

Listing 9.12. Creating an XElement using the imperative construction model provided by LINQ to XML

Listing 9.13. Creating an XML tree using LINQ to XML

Listing 9.14. Creating an XElement with a full XML name and an XNamespace

Listing 9.15. Creating several elements that all use an XNamespace

Listing 9.16. Associating a prefix with a namespace

Listing 9.17. Creating XML with an attribute

Listing 9.18. Creating XML using Visual Basic and functional construction

Listing 9.19. Creating XML using XML literals

Listing 9.20. Embedding expressions in XML literal expression holes

Listing 9.21. Using expression holes to populate the element name of an XML element

Listing 9.22. Create an XML document using the XDocument class and functional construction

Listing 9.23. Create an XML document with an XML stylesheet processing instruction

Listing 9.24. Create an HTML document with a document type via the XDocumentType class

Listing 9.25. Add content to an XElement using the Add method

Listing 9.26. Removing one or many elements from an XElement with Remove

Listing 9.27. Replacing the contents of a element with new content

Listing 9.28. Replace an entire node with ReplaceWith

Listing 9.29. Saving an XElement to disk with the Save method

Chapter 10. Query and transform XML with LINQ to XML

Listing 10.1. A sample XML file, illustrating the tree-like structure of XML

Listing 10.2. Selecting an element by name using the Element query axis method

Listing 10.3. Retrieve an attribute from an XML element with the Attribute method

Listing 10.4. Select all the child book elements using the Elements query axis method

Listing 10.5. Retrieve every book within the XML with the Descendants method

Listing 10.6. Comparing the Descendants and DescendantsAndSelf query axis methods

Listing 10.7. Using LINQ query expression syntax for querying XML

Listing 10.8. Using Ancestors to query an XML document for elements above a particular element

Listing 10.9. Finding all element nodes at the same level as an element using ElementsBeforeSelf

Listing 10.10. The RSS feed that we’ll query using the Visual Basic XML axis properties

Listing 10.11. Querying an RSS feed for all items using the Elements query axis method

Listing 10.12. Querying an RSS feed for all items using the child axis property

Listing 10.13. Retrieving all descendant nodes with the descendants axis property

Listing 10.14. Using the Value extension property to return the value of the first XElement

Listing 10.15. Selecting the value of an attribute using the attribute axis property

Listing 10.16. Using the Select standard query operator to apply a projection to an XML document

Listing 10.17. Calling the Select standard query operator using LINQ query expression syntax

Listing 10.18. Load XML from Amazon.com and filter the book list using the where clause

Listing 10.19. Ordering the results of a query using the orderby expression

Listing 10.20. Grouping the results of a query using the group expression

Listing 10.21. Querying XElement objects with XPath

Listing 10.22. The XML to be transformed

Listing 10.23. The LINQ to XML code created via the Paste XML as LINQ Visual Studio .NET add in

Listing 10.24. Retrieve the title, publisher, and authors for each book within the XML

Listing 10.25. Transform XML into XHTML with LINQ to XML transformations

Listing 10.26. Transforming an XElement using XSLT

Listing 10.27. An extension method for transforming an XNode using XSL

Chapter 11. Common LINQ to XML scenarios

Listing 11.1. Book data in XML format

Listing 11.2. Create Book objects from the XML using object initializers

Listing 11.3. Creating objects from XML

Listing 11.4. The SampleData class introduced in chapter 4

Listing 11.5. Limit the set of books returned to only those with reviews

Listing 11.6. XML literals template for building RSS

Listing 11.7. Creating XML from an object graph using XML literals

Listing 11.8. The XML that will be created from our database

Listing 11.9. Stub code via copy and paste XElement

Listing 11.10. Retrieve the data necessary for building our XML document using LINQ to XML queries

Listing 11.11. Code to create our full XML tree

Listing 11.12. Load XML from Amazon’s e-commerce web service

Listing 11.13. Mixing XML and relational data within a single query

Listing 11.14. Amazon.com REST URL for retrieving books by a keyword

Listing 11.15. Amazon XML for a book

Listing 11.16. When Search button is clicked, query Amazon for books matching our keywords

Listing 11.17. Read the values out of the XML returned by Amazon into our Book objects

Listing 11.18. Joining the results returned by Amazon with the books selected in the grid

Listing 11.19. Populate the book publisher by joining to the Publisher table

Listing 11.20. Query our XML for the authors of the book

Listing 11.21. Retrieving the authors from the XML and converting them into an EntitySet

Listing 11.22. Full code for importing books from Amazon.com

Listing 11.23. CSV of Books

Listing 11.24. The XML output that will be created from the transformation

Listing 11.25. Read the lines from the text file into XElement objects

Listing 11.26. Flat file to XML implementation

Chapter 12. Extending LINQ

Listing 12.1. Standard implementation of the Sum operator for int

Listing 12.2. projectsSumExtensions.csSumExtensions.csLongSum, improved implementation of the Sum operator for int (SumExtensions.cs)

Listing 12.3. TotalPrice custom query operator (CustomQueryOperators.cs)

Listing 12.4. Min custom query operator (CustomQueryOperators.cs)

Listing 12.5. Books custom query operator (CustomQueryOperators.cs)

Listing 12.6. IsExpensive custom query operator (CustomQueryOperators.cs)

Listing 12.7. The query expression pattern

Listing 12.8. Custom implementations of Where and Select with the standard generic signatures (CustomImplementation.csproj)

Listing 12.9. Domain-specific implementations of Where and Select (DomainSpecificOperators.cs)

Listing 12.10. Implementation of GroupJoin for a single element (NonSequenceOperator.cs)

Listing 12.11. Sample expression tree generated for a LINQ to Amazon query

Chapter 13. LINQ in every layer

Listing 13.1. Data access object with a method that returns a query (IQueryable<T>)

Listing 13.2. Data access object with a method that returns a collection of objects (List<T>)

Listing 13.3. DAL method returning subjects ordered by name, with lazy loading disabled

Listing 13.4. Retrieving a list of publishers from a database and binding it to a GridView

Listing 13.5. ASP.NET markup to display a list of publishers in a GridView

Listing 13.6. Handler for the GridView.RowDataBound event used to display a child collection (Publisher.Books)

Listing 13.7. Handler for LinqDataSource.Selecting to provide the query used by the DataSource

Listing 13.8. Markup to display a list of books in a GridView

Listing 13.9. Using an anonymous type and query operators to shape a list of books for display

Listing 13.10. LINQ to XML query used to prepare data to be inserted into a database

Listing 13.11. Web method that creates an RSS feed and returns it as an XmlDocument (RSS.asmx.cs)

Listing 13.12. Loading the complete data from the database into a typed DataSet (GetXML.ashx.cs)

Listing 13.13. Filtering and displaying data from a DataSet (XMLImportExport.aspx.cs)

Listing 13.14. Filtering and displaying data from a DataSet (XMLImportExport.aspx.cs)

Listing 13.15. Inserting books from a DataSet into a database (XMLImportExport.aspx.cs)

Listing 13.16. Querying Amazon using LINQ to Amazon with dynamic criteria (AddBooks.aspx.cs)

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.12.76.164