<p>I'm trying to get the following bit of code to work in LINQPad but am unable to index into a var. Anybody know how to index into a var in LINQ?</p>

<pre><code>string[] sa = {"one", "two", "three"};
sa[1].Dump();

var va = sa.Select( (a,i) =&gt; new {Line = a, Index = i});
va[1].Dump();
// Cannot apply indexing with [] to an expression of type 'System.Collections.Generic.IEnumerable&lt;AnonymousType#1&gt;'
</code></pre>

[{'answer_id': 44991, 'author': 'Joseph Daigle', 'author_id': 507, 'author_profile': 'https://Stackoverflow.com/users/507', 'pm_score': 5, 'selected': True, 'text': '<p>As the comment says, you cannot apply indexing with <code>[]</code> to an expression of type <code>System.Collections.Generic.IEnumerable&lt;T&gt;</code>. The IEnumerable interface only supports the method <code>GetEnumerator()</code>. However with LINQ you can call the extension method <code>ElementAt(int)</code>.</p>\n'}, {'answer_id': 45944, 'author': 'Keith', 'author_id': 905, 'author_profile': 'https://Stackoverflow.com/users/905', 'pm_score': 2, 'selected': False, 'text': '<p>You can\'t apply an index to a var unless it\'s an indexable type:</p>\n\n<pre><code>//works because under the hood the C# compiler has converted var to string[]\nvar arrayVar = {"one", "two", "three"};\narrayVar[1].Dump();\n\n//now let\'s try\nvar selectVar = arrayVar.Select( (a,i) =&gt; new { Line = a });\n\n//or this (I find this syntax easier, but either works)\nvar selectVar =\n    from s in arrayVar \n    select new { Line = s };\n</code></pre>\n\n<p>In both these cases <code>selectVar</code> is actually <code>IEnumerable&lt;\'a&gt;</code> - not an indexed type. You can easily convert it to one though:</p>\n\n<pre><code>//convert it to a List&lt;\'a&gt;\nvar aList = selectVar.ToList();\n\n//convert it to a \'a[]\nvar anArray = selectVar.ToArray();\n\n//or even a Dictionary&lt;string,\'a&gt;\nvar aDictionary = selectVar.ToDictionary( x =&gt; x.Line );\n</code></pre>\n'}]
['https://Stackoverflow.com/questions/44989', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/1463/']