How can I increment counter
while inside a LINQ query?
Consider the following
Public Class SimpleString
Public Property Value As String
End Class
...
Public Shared Sub SetStuff()
Dim stringList As IEnumerable(Of SimpleString) =
{New SimpleString With {.Value = "0"},
New SimpleString With {.Value = "0"},
New SimpleString With {.Value = "0"},
New SimpleString With {.Value = "0"},
New SimpleString With {.Value = "0"}}
Dim counter As Integer = 0
Dim newIntegerList As IEnumerable(Of SimpleString) =
(From i In stringList
Select New SimpleString With
{
.Value = (counter = counter + 1).ToString
})
End Sub
The above does not work.
Rules:
- No C# (I know it can be done in C#, but this is VB.NET)
- No method syntax (my query is complex, so query syntax is preferable)
- No ToList or List(Of) (I don't use Lists anywhere in my application and I'd rather not start for consistency. Also IEnumerable means queries are not executed until necessary)
- No loops (I'm not using concrete classes. Also consistency as I minimise the use of loops and primarily use LINQ)