Mega Code Archive

 
Categories / C# Book / 04 LINQ
 

0414 Indexed projection

The select operator can accept an integer argument as an indexer. This works only with local queries: using System; using System.Collections; using System.Collections.Generic; using System.Linq; class Program { static void Main() { string[] names = { "Java", "C#", "Javascript", "SQL", "Oracle", "Python", "C++", "C", "HTML", "CSS" }; IEnumerable<string> query = names.Select((s, i) => i + "=" + s); foreach(String s in query){ Console.WriteLine(s); } } } The output: 0=Java 1=C# 2=Javascript 3=SQL 4=Oracle 5=Python 6=C++ 7=C 8=HTML 9=CSS