.NET version 4.0, language: C# (VS 2010)
C# "fluent" syntax for LINQ gives the following option for Select, where the predicate has two arguments - the item in the list itself, and the index of the item in the list.
Very stripped out generic example of the kind of thing I'm doing:
List<string> MyList = new List<string>{ "foo", "bar", "baz" };
var nodes = MyList.Select(
(item, indexInList) =>
new XElement("p", new XAttribute("id", indexInList + 1 ), item)
);
output is XElements representing
<p id=1>foo</p>
<p id=2>bar</p>
<p id=3>baz</p>
This works, meets requirements, no issues there. However. Our department standards prefer query syntax, and personally I find it easier to read. Is it possible to express the above in query syntax?
My instinct was to try the following by analogy but VS didn't like it at all.
var nodes = from (item, indexInList) in MyList
select new XElement("p", new XAttribute("id", indexInList + 1), item);
I've been googling back and forth and I keep getting questions about "given XYZ quantifiers, find the index of a single matching item" - I don't want that. I want one-or-many matching items and their indexes.