How to access index in IEnumerable object in C#?

后端 未结 5 1549
-上瘾入骨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 18:57

    foreach(var indexedProduct in Model.Products.Select((p, i)=> new {Product = p, Index = i})
    {
       ...
       ...indexedProduct.Product...
       ...indexProduct.Index ...//this is what you need.
       ...
    }
    

提交回复
热议问题