Select - Transforming elements C# with Example



Select - Transforming elements C# with Example

Select allows you to apply a transformation to every element in any data structure implementing IEnumerable. 
Getting the first character of each string in the following list: 
List trees = new List{ "Oak", "Birch", "Beech", "Elm", "Hazel", "Maple" }; 
Using regular (lambda) syntax 
//The below select stament transforms each element in tree into its first character. 
IEnumerable initials = trees.Select(tree => tree.Substring(0, 1)); 
foreach (String initial in initials) { 
System.Console.WriteLine(initial); 
} 
Output: 
O 
B 
B 
E 
H 
M 
Live Demo on .NET Fiddle 
Using LINQ Query Syntax 
initials = from tree in trees 
select tree.Substring(0, 1); 
 

0 Comment's

Comment Form

Submit Comment