Tag Archives: LINQ

LINQ Row Number in Visual Basic (VB .Net)

I needed a way to have an incrementing number column in a row. I was using an object so I couldn’t do some fancy tsql. Below is the story of my solution. If you know of something more elegant then please put it in the comments. I found a good example of a c# solution but I couldn’t get it to work in VB. Here is the c# code:

int i = 1;
return from author in DataContext.Authors
    select new Author() { Lp = i++, Name = author.Name };

The problem is that VB does not support the incrementer ++ like c#. I tried the i += 1 but I could not get the syntax to work. I searched around and no one had a good suggestion. I could not find a way to increment and return the same variable in the same lambda. I finally decided to make my own function the could store the variable and the logic to increment it when it is called. The function looked like:

Public Shared Function increment() As Integer
     Static i As Integer
     i = i + 1
     Return i
End Function

While the Query was:

Dim Characters As Char()
Characters = MyString.ToCharArray
'Query out the characters and positions
Dim q1 = From c In Characters _
        Select MyChar = c.ToString, Position = Increment()

This all seemed to work until I ran two tests in a row. The incremented numbers kept going higher on the second test. I realized that the static integer stayed in scope for too long. I had to have a way for it to maintain scope properly. It needed to be it’s own class so it could be substantiated and disposed of properly. Here is the new class:

Public Class IncrementerZeroBased
    'Start at -1 so first time returned from loop it equals 0
    Private i As Integer = -1
    Public Function increment() As Integer
        i = i + 1
        Return i
    End Function
End Class

And the new LINQ Query:

Dim Characters As Char()
Characters = Mask.ToCharArray
Dim i As New IncrementerZeroBased

'Query out the characters and positions
Dim q1 = From c In Characters _
    Select MyChar = c.ToString, Position = i.increment

This all seems to work quite well. There might be a more elegant way. Please comment if you have any ideas suggestions.




I needed a way to have an incrementing number column in a row. I was using an object so I couldn’t do some fancy tsql. Below is the story of my solution. If you know of something more elegant then please put it in the comments. I found a good example of a c# solution but I couldn’t get it to work in VB. Here is the c# code:

int i = 1;
return from author in DataContext.Authors
    select new Author() { Lp = i++, Name = author.Name };

The problem is that VB does not support the incrementer ++ like c#. I tried the i += 1 but I could not get the syntax to work. I searched around and no one had a good suggestion. I could not find a way to increment and return the same variable in the same lambda. I finally decided to make my own function the could store the variable and the logic to increment it when it is called. The function looked like:

Public Shared Function increment() As Integer
     Static i As Integer
     i = i + 1
     Return i
End Function

While the Query was:

Dim Characters As Char()
Characters = MyString.ToCharArray
'Query out the characters and positions
Dim q1 = From c In Characters _
        Select MyChar = c.ToString, Position = Increment()

This all seemed to work until I ran two tests in a row. The incremented numbers kept going higher on the second test. I realized that the static integer stayed in scope for too long. I had to have a way for it to maintain scope properly. It needed to be it’s own class so it could be substantiated and disposed of properly. Here is the new class:

Public Class IncrementerZeroBased
    'Start at -1 so first time returned from loop it equals 0
    Private i As Integer = -1
    Public Function increment() As Integer
        i = i + 1
        Return i
    End Function
End Class

And the new LINQ Query:

Dim Characters As Char()
Characters = Mask.ToCharArray
Dim i As New IncrementerZeroBased

'Query out the characters and positions
Dim q1 = From c In Characters _
    Select MyChar = c.ToString, Position = i.increment

This all seems to work quite well. There might be a more elegant way. Please comment if you have any ideas suggestions.