Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/313.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# IEnumerable类型中的最佳访问方式元素_C# - Fatal编程技术网

C# IEnumerable类型中的最佳访问方式元素

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

我使用的语言是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=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的理想候选元素。我需要元素的索引。