I have an array of Car objects and using the following piece of code I create an XML Document from these objects. I have set up a counter variable i
to be able to index the Car elements in the document. Is there a different way of obtaining the index of the currently processed element?
int i = 0;
XDocument doc =
new XDocument(
new XElement(
"Inventory",
from car in cars
select
new XElement("Car",
new XAttribute("ID", ++i), //<<== index here
new XElement("Color", car.Color),
new XElement("Make", car.Make),
new XElement("PetName", car.PetName)
)
)
);
The approach I have taken works fine, I am just wondering whether there's a magic word or extension method that will yield the index without my incrementing a counter variable?