How to access index in IEnumerable object in C#?

后端 未结 5 1550
-上瘾入骨i
-上瘾入骨i 2020-12-18 18:12

I have an IEnumerable object. I would like to access based on index for instance:

for(i=0; i<=Model.Products; i++)
{
      ???
}

Is this

5条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2020-12-18 19:01

    There is no index in IEnumerator. Use

    foreach(var item in Model.Products)
    {
       ...item...
    }
    

    you can make your own index if you want:

    int i=0;
    foreach(var item in Model.Products)
    {
        ... item...
        i++;
    }
    

提交回复
热议问题