// Get everything in reverse.
Console.WriteLine("All cars in reverse:");
var subset = (from c in myCars select c).Reverse();
foreach (Car c in subset)
{
Console.WriteLine("{0} is going {1} MPH", c.PetName, c.Speed);
}
}
Here, we called the Reverse() method at the time we constructed our query. Again, as an
alternative, we could invoke this method on the myCars array as follows:
static void ReversedSelection(Car[] myCars)
{
// Get everything in reverse.
Console.WriteLine("All cars in reverse:");
var subset = from c in myCars select c;
foreach (Car c in subset.Reverse())
{
Console.WriteLine(c.ToString());
}
}
Sorting Expressions
As you have seen over this chapter’s initial examples, a query expression can take an orderby
operator to sort items in the subset by a specific value. By default, the order will be ascending; thus,
ordering by a string would be alphabetical, ordering by numerical data would be lowest to highest,
and so forth. If you wish to view the results in a descending order, simply include the descending
operator. Ponder the following method:
static void OrderedResults(Car[] myCars)
{
Do'stlaringiz bilan baham: |