Sunday, April 25, 2010

LINQ Samples (Projection Operators) : Example 1

Example 1 : Provide a simple select which will allow to do a select on the list of string with a simple modification

private void SimpleSelectOne()
{
Console.WriteLine("Simple Select operations \n");
string[] strTechnology = new string[] { "C#","asp.net","vb.net","j#","f#","Cobol.net","LINQ","WCF","WPF","Silverlight" };

Console.WriteLine("\nBefore LINQ :");
foreach (string s in strTechnology)
{
Console.WriteLine("{0}", s);
}

Console.WriteLine("\nAfter LINQ transform : ");
var strList = strTechnology.Select( a => "MS " + a );
foreach (string s in strList)
{
Console.WriteLine("{0}",s);
}
}


OUTPUT :

BEFORE LINQ :
C#
asp.net
vb.net
j#
f#
Cobol.net
LINQ
WCF
WPF
Silverlight

AFTER LINQ transform :
MS C#
MS asp.net
MS vb.net
MS j#
MS f#
MS Cobol.net
MS LINQ
MS WCF
MS WPF
MS Silverlight


You can see that the statement var strList = strTechnology.Select( a => "MS " + a ); has added a string "MS" to existing string. Let it be reminded that the original string remains the same.