C# IEnumerable类型中的最佳访问方式元素
我使用的语言是C 假设我们有一个IEnumerable,它是linq查询的结果。T是匿名类型。 如果我们想访问这种类型的元素,最好的方法是什么 我认为有两种方法 1) 将IEnumerable转换为List,然后访问元素C# IEnumerable类型中的最佳访问方式元素,c#,C#,我使用的语言是C 假设我们有一个IEnumerable,它是linq查询的结果。T是匿名类型。 如果我们想访问这种类型的元素,最好的方法是什么 我认为有两种方法 1) 将IEnumerable转换为List,然后访问元素 List<T> list = result.ToList(); for(int rowIndex=0; rowIndex<list.Count; rowIndex++) { double x = list[rowIndex].val; } List
List<T> list = result.ToList();
for(int rowIndex=0; rowIndex<list.Count; rowIndex++)
{
double x = list[rowIndex].val;
}
List List=result.ToList();
对于(int rowIndex=0;rowIndex您不需要创建集合,请使用int
变量0,并在foreach
中增加它。然后您就有了索引和元素
int index = 0;
foreach(var x in result)
{
// x is the element and index the current index
Console.WriteLine("Val:{0} Index:{1}", x.val, index);
index ++;
}
您不需要创建集合,使用int
变量0并在foreach
中增加它。然后您就有了索引和元素
int index = 0;
foreach(var x in result)
{
// x is the element and index the current index
Console.WriteLine("Val:{0} Index:{1}", x.val, index);
index ++;
}
我仍然不理解这个问题。您不需要创建集合,使用int index=0
并在foreach
中增加它。然后您就有了索引和元素。使用IEnumerable
的foreach
有什么问题吗?t
怎么可能是匿名类型?这听起来像是@crashmstr我需要元素的索引。我仍然不理解这个问题。你不需要创建集合,使用int index=0
并在foreach
中增加它。那么你就有了索引和元素。使用IEnumerable
的foreach
有什么问题吗?t
怎么可能是匿名的type?这听起来是@crashmstr的理想候选元素。我需要元素的索引。