// Now get only the names of the cars.
Console.WriteLine("Only PetNames:");
var names = from c in myCars select c.PetName;
foreach (var n in names)
{
Console.WriteLine("Name: {0}", n);
}
In this case, names is really an internal type that implements IEnumerable, given that
we are selecting only the values of the PetName property for each Car object. Again, using implicit
typing via the var keyword, our coding task is simplified.
Now consider the following task. What if you’d like to obtain and display the makes of each
vehicle? If you author the following query expression:
var makes = from c in myCars select c.Make;
you will end up with a number of redundant listings, as you will find BMW, Ford, and VW
accounted for multiple times. You can use the Enumerable.Distinct() method to eliminate
such duplication:
C H A P T E R 1 4
Do'stlaringiz bilan baham: |