FREE BOOK

Chapter 11: An introduction to LINQ

Posted by Murach Free Book | LINQ August 13, 2009
In this chapter, you’ll learn the basic concepts and skills for using a new feature of C# 2008 called LINQ. To illustrate these concepts and skills, I’ll use an implementation of LINQ called LINQ to Objects. You use LINQ to Objects to work with in-memory data structures such as generic lists and arrays.

Perspective

In this chapter, you learned the basic skills for coding and executing LINQ queries that work with generic lists. With these skills, you should be able to create queries that perform a variety of functions. However, there's a lot more to learn about LINQ than what's presented here. In particular, you'll want to learn about the three implementations of LINQ for ADO.NET. You'll learn about two of these implementations, LINQ to DataSet and LINQ to SQL, in the next three chapters. Then, in chapter 18, you'll learn how to use LINQ to Entities, which uses the new Entity Framework.

Terms

Language-Integrated Query (LINQ) LINQ to XML
object-relational mapping deferred execution
query operator query variable
extension method immediate execution
method-based query range variable
query expression projection
enumerable type object initializer
LINQ to Objects anonymous type
LINQ to DataSet alias
LINQ to SQL lambda expression
LINQ to Entities lambda operator
LINQ to ADO.NET delegate

Exercise 11-1 Create the Vendor Balances application

In this exercise, you'll develop and test the Vendor Balances application that was presented in this chapter.

Design the form

  1. Open the project that's in the C:\ADO.NET 3.5 C#\Chapter 11\DisplayVendorsDue directory. In addition to the default form, this project
    contains the business and database classes needed by the application.
  2. Add a ListView control to the form, and set the View property of this control to Details.
  3. Use the smart tag menu for the ListView control to display the ColumnHeader Collection Editor. Then, define the column headings for this control so they look like the ones shown in figure 11-14.

Add code to display the invoice data

  1. Open the Invoice, InvoiceDB, and PayablesDB classes and review the code that they contain. In particular, notice that the GetInvoices method in the InvoiceDB class returns the invoices in a List<Invoice> object.
  2. Add an event handler for the Load event of the form. Then, use the GetInvoices method to get the list of invoices, and store this list in a variable.
  3. Define a query expression that returns all the invoices in the invoice list that have a balance due greater than zero. Sort the invoices by balance due in descending sequence within vendor ID.
  4. Use a foreach statement to execute the query and load the results into the ListView control.
  5. Run the application to see how it works. At this point, the list should include one item for each invoice with a balance due. Make any necessary corrections, and then end the application.

Enhance the application to display the vendor names

  1. Open the Vendor and VendorDB classes and review the code they contain. In particular, notice that the GetVendors method in the VendorDB class returns the vendors in a List<Vendor> object.
  2. Add code at the beginning of the Load event handler that uses the GetVendors method to get the list of vendors, and store this list in a variable.
  3. Modify the query expression so it joins the data in the vendor list with the data in the invoice list, so it sorts the results by balance due in descending sequence within vendor name, and so only the fields that are needed by the form are returned by the query.
  4. Modify the foreach statement so it adds the vendor name instead of the vendor ID to the ListView control.
  5. Run the application to make sure it works correctly. Although the list will still include one item for each invoice with a balance due, the items will be listed by vendor name instead of vendor ID.

Enhance the application to group the invoices by vendor

  1. Modify the query expression so it groups the invoices by vendor name. Then, use the Sum method in the where clause to calculate the balance due for each vendor so that only vendors with a balance due are included in the query results. Use the Sum method in the orderby clause to sort the vendor balances in descending sequence. (Be sure to omit the vendor name from the sort sequence since it's no longer needed.) And use the Sum method in the select clause to include the balance due for each vendor in the query results.
  2. Run the application to make sure it works correctly. If it does, the form should look like the one shown in figure 11-14. When you're done, close the solution.

Total Pages : 10 678910

comments