CSCI 3328 Object Oriented Programming in C Chapter

CSCI 3328 Object Oriented Programming in C# Chapter 8: LINQ and Generic Collections – Exercises Xiang Lian The University of Texas – Pan American Edinburg, TX 78539 lianx@utpa. edu 1

Objectives • In this chapter, you will do some exercises related to: – LINQ – Collections 2

Multiple Choices • Use the _______ property of the Array class to find the number of elements in the List. – A. Get. Length B. Get. Length() 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 3

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 4

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 5

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, my. Array, in decreasing order, we can use the following LINQ query: – var sorted = from e in my. Array order by e descending select e • To add an integer to a List variable, list. Scores, we can use: – list. Scores. Items. Add(); 6

Write a Program • Assume a class Invoice includes 4 properties: – – Part. Number (int) Part. Description (string) Quantity of the item being purchased (int) Price (decimal) • Write a program that performs queries over the array, Invoices, of Invoice objects: – 1. Use LINQ to sort the Invoice objects by Part. Description; – 2. Use LINQ to sort the Invoice objects by Price; – 3. Use LINQ to select the Part. Description and Quantity, and sort the results by Quantity; – 4. Use LINQ to select from each Invoice the Part. Description and the value of Invoice (i. e. , Quantity*Price); – 5. Use the results of the LINQ query in Part 4, select the Invoice. Total in the range $200 to $500. 7

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 8

Write a Program (cont'd) • Declare an array, num. Array, 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 9

10
- Slides: 10