C#大学课程(第五版)课后习题9.3查询Invoice对象的数组
/*9.3
(查询Invoice对象的数组)利用本章例子的ex_09_03文件夹下提供的Invoice类,创建一个Invoice对象的数组。自定义数量。Invoice类中包含4个属性:部件编号(PartNumber,int类型)、部件描述(PartDescription,string类型)、要购买的数量(Quantity,int类型)以及单价(Price,decimal类型)。对Invoice对象数组执行下列查询并显示结果。
a)使用LINQ按照PartDescription排序Invoice对象。
b)使用LINQ按照Price排序Invoice对象。
c)使用LINQ选择PartDescription和Quantity,并按Quantity排序结果。
d)用LINQ从每个Invoice中选择PartDescription以及Invoice的值(即Quantity+Price)。将所计算的列命名为InvoiceTotal。根据Invoice的值排序结果。[提示:在一个新的范围变量total中用let字句保存Quantity+Price的结果。]
e)用d)中查询的结果,选择200~500美元的InvoiceTotal。
*/
public class Invoice
{
private int quantityValue;
private decimal priceValue;
public int PartNumber { get; set; }
public string PartDescription { get; set; }
public Invoice( int part, string description,
int count, decimal pricePerItem )
{
PartNumber = part;
PartDescription = description;
Quantity = count;
Price = pricePerItem;
}
public int Quantity
{
get
{
return quantityValue;
}
set
{
if ( value > 0 )
quantityValue = value;
}
}
public decimal Price
{
get
{
return priceValue;
}
set
{
if ( value >= 0M )
priceValue = value;
}
}
public override string ToString()
{
return string.Format( "{0,-5} {1,-20} {2,-5} {3,6:C}",
PartNumber, PartDescription, Quantity, Price );
}
}
using System;
using System.Linq;
using System.Collections.Generic;
public class LINQInvoiceArray
{
public static void Main( string[] args )
{
Invoice[] invoices = {
new Invoice( 83, "Electric sander", 7, 57.98M ),
new Invoice( 24, "Power saw", 18, 99.99M ),
new Invoice( 7, "Sledge hammer", 11, 21.5M ),
new Invoice( 77, "Hammer", 76, 11.99M ),
new Invoice( 39, "Lawn mower", 3, 79.5M ),
new Invoice( 68, "Screwdriver", 106, 6.99M ),
new Invoice( 56, "Jig saw", 21, 11M ),
new Invoice( 3, "Wrench", 34, 7.5M ) };
var sortedByDescription =
from item in invoices
orderby item.PartDescription
select item;
Console.WriteLine( "Sorted by description:" );
foreach ( var item in sortedByDescription )
Console.WriteLine( item );
var sortedByPrice =
from item in invoices
orderby item.Price
select item;
Console.WriteLine( "\nSorted by price:" );
foreach ( var item in sortedByPrice )
Console.WriteLine( item );
var descriptionAndQuantity =
from item in invoices
orderby item.Quantity
select new { item.PartDescription, item.Quantity };
Console.WriteLine(
"\nSelect description and quantity, sort by quantity:" );
foreach ( var item in descriptionAndQuantity )
Console.WriteLine( item );
var descriptionAndTotal =
from item in invoices
let total = item.Quantity * item.Price
orderby total
select new { item.PartDescription, InvoiceTotal = total };
Console.WriteLine(
"\nSelect description and invoice total, sort by invoice total:" );
foreach ( var item in descriptionAndTotal )
Console.WriteLine( item );
var totalBetween200And500 =
from item in descriptionAndTotal
where item.InvoiceTotal > 200M && item.InvoiceTotal < 500M
select item;
Console.WriteLine( string.Format(
"\nInvoice totals between {0:C} and {1:C}:", 200, 500 ) );
foreach ( var item in totalBetween200And500 )
Console.WriteLine( item );
Console.WriteLine();
}
}