如何在LINQ中索引变量?

Guy*_*Guy 10 c# linq

我试图让以下一些代码在LINQPad中工作,但我无法索引到var.任何人都知道如何索引LINQ中的var?

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

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

Jos*_*gle 21

正如评论所说,您不能将索引应用于[]类型的表达式System.Collections.Generic.IEnumerable<T>.IEnumerable接口仅支持该方法GetEnumerator().但是使用LINQ,您可以调用扩展方法ElementAt(int).