100 likes | 111 Views
CSCI 3328 Object Oriented Programming in C# Chapter 8: LINQ and Generic Collections – Exercises. UTPA – Fall 2012. Objectives. In this chapter, you will do some exercises related to: LINQ Collections. Multiple Choices.
E N D
CSCI 3328 Object Oriented Programming in C# Chapter 8: LINQ and Generic Collections – Exercises UTPA – Fall 2012
Objectives • In this chapter, you will do some exercises related to: • LINQ • Collections
Multiple Choices • Use the _______ property of the List class to find the number of elements in the List. • A. GetLength B. GetLength() C. Length D. Length() • An array’s length is ______. • A. one more than the array’s last index B. one less than the array’s last index C. the same as the array’s last index D. returned by size() – 1 • The process of ordering the elements of an array is called ____ the array. • A. allocating B. sorting C. declaring D. initializing • The LINQ ________clause is used for filtering. • A. orderby B. from … in C. where D. select • _______are classes specifically designed to store groups of objects and provide methods that organize, store and retrieve those objects. • A. Int B. Classes C. Collections D. Objects
Multiple Choices (cont'd) • To add an element to the end of a List, use the _____ method. • A. insert B. Items C. add D. Item • To get only unique results from a LINQ query, use the ______ method. • A. Unique B. Only C. Distinct D. Solely • To sort the elements of an LINQ query, use ___ clause. • A. orderby B. ascending C. where D. select • Which extension method can determine the number of elements in the result of an LINQ query? • A. Sum B. Distinct C. Length D. Count
True/False Statements • The where clause in an LINQ query is optional • The orderby clause of an LINQ query by default sorts the array in descending order • LINQ can query arrays of only primitive types • LINQ queries can be used on both arrays and collections • The Remove method of the List class removes an element at a specific index
True/False Statements (cont'd) • To declare an integer array of two rows and five columns, the expression should be: • int[] a = new int [3, 6]; • To sort an integer array, myArray, in decreasing order, we can use the following LINQ query: • var sorted = from e in myArray order by e descending select e • To add an integer to a List variable, listScores, we can use: • listScores.Items.Add();
Write a Program • Assume a class Invoice includes 4 properties: • PartNumber (int) • PartDescription (string) • Quantity of the item being purchased (int) • Price (decimal) • Write a program that performs queries over the array of Invoices objects: • 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 Invoice (i.e., Quantity*Price); • 5. Use the results of the LINQ query in Part 4, select the InvoiceTotal in the range $200 to $500.
Write a Program (cont'd) • Write a console application program that inserts 30 random letters into a List <char>. Perform the following queries on the List and display your results: • 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
Write a Program (cont'd) • Declare an array, numArray, to hold 100 integer numbers • Assign random numbers within [1, 100] into this array • Use the LINQ query to sort this array • Use the LINQ query to check whether a number 50 exists in this array