diff --git a/chap5_essential_language_features/essential_csharp_features/Program.cs b/chap5_essential_language_features/essential_csharp_features/Program.cs index aa82ade..cb8bc45 100644 --- a/chap5_essential_language_features/essential_csharp_features/Program.cs +++ b/chap5_essential_language_features/essential_csharp_features/Program.cs @@ -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.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; + } + } } }