Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Book --> Visual C# How to Program (6th Edition) 1. Exercise 9.3 on page 392-393.

ID: 3717535 • Letter: B

Question

Book --> Visual C# How to Program (6th Edition)

1. Exercise 9.3 on page 392-393.

Please answer this problem and post it here.

b) c) The Remove method of the List class removes an element at a LINQ e Rmthod of the List class removes an element at a specific indesx. Answers to Self-Review Exercises 9.1 a) Count. b) where. c) Collections. d) Add. e) Distinct. 9.2 a) False. The descending modifier is used b) True. c) False. Remove removes the first element equal to its argument. Re is used to make orderby sort in descending order moveAt removes the el ement at a specific index. Exercises 9.3 (Querying an Array ofInvoice Objects) Use the class Invoice provided in the ex09 03 fold er 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 (ype 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 a) Use LINQ to sort the Invoice objects by PartDescription. b) Use LINQ to sort the Invoice objects by Price. c) Use LINQ to select the PartDescription and Quantity and sort the resuls by Quantity.

Explanation / Answer

//Import the namespace for basic support and create the Linq queries.
//namespaces for basic type and Linq support
using System;
using System.Linq;
Define the namespace for holding the application.
//namespace for holding the application
namespace _10201_9_3E
{
Create the class to make and fill the array of invoices.
//class to create invoices
class InvoiceArray
{
// Define the variables to show the attributes of the invoice.
//variable to hold the details of the invoice
public int pNumber;
public string pDesc;
public int pQty;
public double pRate;
// Define the default constructor to initialize the invoice
details.
/* default constructor to initialize objects
*/
public InvoiceArray()
{
}
// Define the parameterized constructor to give the details to an invoice object.
/* Constructor to initialize the invoice details
* @param pNum is the invoice number
* @param pDes is the details of the purchase
* @pQuan is the number of items
* @pPrice is the rate of the item
*/
public InvoiceArray(int pNum, string pDes, int
pQuan,double pPrice)
{
//Assign the values of the parameter to the invoice attributes.
//assign the details to the invoice
pNumber = pNum;
pDesc = pDes;
pQty = pQuan;
pRate = pPrice;
}
//Define the method to create the array of invoices. The method would return the array after creating it.
/* Method to create and initialize the invoices
* @return an array holding all of the invoices
*/
public InvoiceArray[] GetInvoices()
{
//Define and create different objects to make different invoices.
//create the invoices
InvoiceArray inv1 = new InvoiceArray(83,
"Electric sender", 7, 57.98);
InvoiceArray inv2 = new InvoiceArray(24, "Power
saw", 18, 99.99);
InvoiceArray inv3 = new InvoiceArray(7, "Sledge
hammer", 11, 21.50);
InvoiceArray inv4 = new InvoiceArray(77,
"hammer", 76, 11.99);
InvoiceArray inv5 = new InvoiceArray(39, "lawn
mower", 3, 79.50);
InvoiceArray inv6 = new InvoiceArray(68,
"Screwdriver", 106, 6.99);
InvoiceArray inv7 = new InvoiceArray(56,
"Jigsaw", 21, 11.00);
InvoiceArray inv8 = new InvoiceArray(3,
"Wrench", 34, 7.50);
Move all of the invoice objects to the array.
//create the array of invoices
InvoiceArray[] invArr = { inv1, inv2, inv3,
inv4, inv5, inv6, inv7, inv8 };
//return the array
return invArr;
}
}
Create the class to test the invoices application.
//create the class to test the invoice application
class LinqInvoice
{
Define the Main() method.
//define Main()
static void Main(string[] args)
{
//Create the objects of the class and call the method to make the invoice’s array.
//Create the invoiceArray class object
InvoiceArray myInvoice = new InvoiceArray();
//call the method to create invoices
InvoiceArray[]getArr= myInvoice.GetInvoices();
//Make a query to sort the invoice objects by their description.
//query to get the invoices in the increasing
//order of description
var sortDesc=from inv in getArr orderby
inv.pDesc select inv;
//Show the header message and show the details of the invoices according to the description they hold.
//Show the header message
Console.WriteLine("Sort by Description:");
Console.WriteLine("---------------------------------------------");
Console.WriteLine("Number Detail
Quantity Price");
Console.WriteLine("---------------------------------------------");
//get and display the sorted invoices
foreach (var myInv in sortDesc)
Console.WriteLine("{0,2} {1,15} {2,10}
{3}", myInv.pNumber,
myInv.pDesc,myInv.pQty,myInv.pRate);
Console.WriteLine("----------------------------
-----------------");
//Make a query to sort the invoice objects by their price.
//query to get the invoices in the increasing
//order of the price
var sortPrice = from inv in getArr orderby
inv.pRate select inv;
//Show the header message and show the details of the invoices according to the price they hold.
//show the header message
Console.WriteLine("Sort by Price:");
Console.WriteLine("---------------------------------------------");
Console.WriteLine("Number Detail Quantity Price");
Console.WriteLine("---------------------------------------------");
//get and display the sorted invoices
foreach (var myInv in sortPrice)
Console.WriteLine("{0,2} {1,15} {2,10} {3}", myInv.pNumber, myInv.pDesc,myInv.pQty, myInv.pRate);
Console.WriteLine("---------------------------------------------");
//Make a query to sort the invoice objects by their description and select only the description and the quantity of the content.
//query to select the description and quantity
//in the increasing order of description
var sortQty = from inv in getArr orderby
inv.pDesc select new { inv.pDesc, inv.pQty };
//Show the header message and show the details of the invoices description and quantity according to the description they hold.
//show the header message
Console.WriteLine("Sort by Description: ");
Console.WriteLine("---------------------------- -----------------");
Console.WriteLine("Detail Quantity");
Console.WriteLine("---------------------------- -----------------");
//get and display the invoice details
foreach (var myInv in sortQty)
Console.WriteLine("{0,15} {1}", myInv.pDesc, myInv.pQty);
Console.WriteLine("---------------------------------------------");
//Make a query to sort the invoice objects by their total price. This would be found by multiplying the quantity with the price of the item.
//query to sort the invoice in increasing order
//by price total
var sortTotal = from inv in getArr let
invoiceTotal=inv.pQty*inv.pRate orderby
invoiceTotal select inv;
//Show the header message and show the details of the invoices according to the total of price they hold.
//show the header message
Console.WriteLine(" Sort by Invoice total: ");
Console.WriteLine("---------------------------------------------");
Console.WriteLine("Number Detail Quantity Price");
Console.WriteLine("---------------------------- -----------------");
//get and display the invoice details
foreach (var myInv in sortTotal)
Console.WriteLine("{0,2} {1,15} {2,10} {3}", myInv.pNumber, myInv.pDesc, myInv.pQty, myInv.pRate);
Console.WriteLine("---------------------------- -----------------");
//Make a query to sort the invoice objects that fall in the range of price from $200 to $500.
//query to sort the invoices in the range of
//$200 to $500
var sortRange = from inv in getArr let total = inv.pQty * inv.pRate where total > 200 && total < 500 orderby total ascending select inv;
// Show the header message and show the details of the invoices that fall in the range of 200 to 500.
//show the header message
Console.WriteLine(" Sort by Range:");
Console.WriteLine("---------------------------------------------");
Console.WriteLine("Number Detail Quantity Price");
Console.WriteLine("---------------------------- -----------------");
//get and display the invoice details
foreach (var myInv in sortRange)
Console.WriteLine("{0,2} {1,15} {2,10} {3}", myInv.pNumber, myInv.pDesc, myInv.pQty, myInv.pRate);
Console.WriteLine("---------------------------------------------");
Console.ReadKey();
}
}
}