Prev | Current Page 20 | Next

Satheesh, N Kumar

"LINQ Quickly"

I don't have to
explicitly define a new class for the new custom shaped structure. Instead, I can use
the anonymous type feature to implicitly define a new type with just two properties
to represent my custom shaped data.
var IcecreamsWithLessPrice =
from ice in icecreamsList
where ice.Price < 10
select new
{
Name = ice.Name,
Price = ice.Price
};
Console.WriteLine("Ice Creams with price less than 10:");
foreach (var icecream in IcecreamsWithLessPrice)
{
Console.WriteLine("{0} is {1}", icecream.Name,
icecream.Price);
}
In this code, I am declaring an anonymous type within the CIT clause in my LINQ
query. The anonymous type has only two properties, Name and Price, whose
property names and values are inferred from the shape of the query.
In the next step, I am referring to the IEnumerable collection of this anonymous
type returned by the query and loop over them and extract the details. This feature
gives it a dynamic language-like flexibility.
Chapter 1
[ 11 ]
Object Initializers
Object initializers lets you assign values to the properties of an objects at the time
of creating the object.


Pages:
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
żetony do pokera śmieszne dowcipy bajka Connie Talbot dakolen