return makesColors; // Nope!
}
Given that return values cannot be implicitly typed, how can we return the makesColors object
to an external caller?
Transforming Query Results to Array Types
When you wish to return projected data to a caller, one approach is to transform the query result
into a standard CLR Array object using the ToArray() extension method. Thus, if we were to
update our query expression as follows:
// Return value is now an Array.
static Array GetProjectedSubset()
{
Car[] myCars = new Car[]{
new Car{ PetName = "Henry", Color = "Silver", Speed = 100, Make = "BMW"},
new Car{ PetName = "Daisy", Color = "Tan", Speed = 90, Make = "BMW"},
new Car{ PetName = "Mary", Color = "Black", Speed = 55, Make = "VW"},
new Car{ PetName = "Clunker", Color = "Rust", Speed = 5, Make = "Yugo"},
new Car{ PetName = "Melvin", Color = "White", Speed = 43, Make = "Ford"}
};
var makesColors = from c in myCars select new { c.Make, c.Color };
Do'stlaringiz bilan baham: |