e.g. 5.27 Using LINQ to Query Data
parent
e2aa8c7cba
commit
bf4a437acd
|
@ -18,23 +18,24 @@ namespace automatically_implemented_properties
|
|||
new Product {Name = "Corner flag", Category = "Soccer", Price = 34.95M}
|
||||
};
|
||||
|
||||
// define the array to hold results
|
||||
Product[] results = new Product[3];
|
||||
// sort the contents of the array
|
||||
Array.Sort(products, (item1, item2) =>
|
||||
{
|
||||
return Comparer<decimal>.Default.Compare(item1.Price, item2.Price);
|
||||
});
|
||||
// get the first three items in the array as the results
|
||||
Array.Copy(products, results, 3);
|
||||
// print out the name
|
||||
foreach (Product p in results)
|
||||
{
|
||||
Console.WriteLine("Item: {0}, Cost: {1}", p.Name, p.Price);
|
||||
}
|
||||
var results = from product in products
|
||||
orderby product.Price descending
|
||||
select new
|
||||
{
|
||||
product.Name,
|
||||
product.Price
|
||||
};
|
||||
|
||||
int count = 0;
|
||||
// print the name
|
||||
// print out the names
|
||||
foreach (var p in results)
|
||||
{
|
||||
Console.WriteLine("Item: {0}, Cost: {1}", p.Name, p.Price);
|
||||
if (++count == 3)
|
||||
{
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue