Exercises

  1. 9.3 (Querying an Array of Invoice Objects) Use the class Invoice provided in the ex09_03 folder with this chapter’s examples to create an array of Invoice objects. Use the sample data shown in Fig. 9.8. Class Invoice includes four properties—a PartNumber (type int), a PartDescription (type string), a Quantity of the item being purchased (type int) and a Price (type decimal). Perform the following queries on the array of Invoice objects and display the results:

    1. Use LINQ to sort the Invoice objects by PartDescription.

    2. Use LINQ to sort the Invoice objects by Price.

    3. Use LINQ to select the PartDescription and Quantity and sort the results by Quantity.

    4. Use LINQ to select from each Invoice the PartDescription and the value of the Invoice (i.e., Quantity * Price). Name the calculated column InvoiceTotal. Order the results by Invoice value. [Hint: Use let to store the result of Quantity * Price in a new range variable total.]

    5. Using the results of the LINQ query in part (d), select the InvoiceTotals in the range $200 to $500.

      Fig. 9.8 Sample data for Exercise 9.3.

      Part number Part description Quantity Price
      83 Electric sander     7 57.98
      24 Power saw   18 99.99
        7 Sledge hammer   11 21.50
      77 Hammer   76 11.99
      39 Lawn mower     3 79.50
      68 Screwdriver 106   6.99
      56 Jig saw   21 11.00
        3 Wrench 34   7.50
  2. 9.4 (Duplicate Word Removal) Write a console app that inputs a sentence from the user (assume no punctuation), then determines and displays the nonduplicate words in alphabetical order. Treat uppercase and lowercase letters the same. [Hint: You can use string method Split with no arguments, as in sentence.Split(), to break a sentence into an array of strings containing the individual words. By default, Split uses spaces as delimiters. Use string method ToLower before calling Split.]

  3. 9.5 (Sorting Letters and Removing Duplicates) Write a console app that inserts 30 random letters into a List<char>. Perform the following queries on the List and display your results: [Hint: Strings can be indexed like arrays to access a character at a specific index.]

    1. Use LINQ to sort the List in ascending order.

    2. Use LINQ to sort the List in descending order.

    3. Display the List in ascending order with duplicates removed.

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

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