Monday, April 26, 2010

LINQ Samples (Projection Operators) : Select Anonymous

Following example is used with Anonymous type for a select operation.It provides an transformed output as a Anonymous type


private void SelectAnonymousTypeOne()
{
string[] strTech = new string[] { "C#", "asp.net", "vb.net", "j#", "f#", "Cobol.net", "LINQ", "WCF", "WPF", "Silverlight" };

var str = strTech.Select(a =>
new { str = a.ToString(),
length = a.Length ,
specialChar = a.EndsWith("#")
});

foreach(var v in str)
{
Console.WriteLine("{0}\n Length:{1} Ends With #{2}",v.str,v.length , v.specialChar );

}
}

OUTPUT

C#
Length:2 Ends with char '#': True
asp.net
Length:7 Ends with char '#': False
vb.net
Length:6 Ends with char '#': False
j#
Length:2 Ends with char '#': True
f#
Length:2 Ends with char '#': True
Cobol.net
Length:9 Ends with char '#': False
LINQ
Length:4 Ends with char '#': False
WCF
Length:3 Ends with char '#': False
WPF
Length:3 Ends with char '#': False
Silverlight
Length:11 Ends with char '#': False