0

normally I am a C# guy, recently I am working on some VB.net code. I just learned that in VB.net you can index into an IEnumerable, a little surprise for me, different from C#.

However, in the following code, I found the indexing not working anymore, I don't know why. What's happening here?

Imports System.Linq
Structure Point
    Public X As Double
    Public Y As Double
End Structure
Class Circle
    Public Property Radius As Double
    Public Property Center As Point
End Class
Module Module1
    Sub Main()
        Dim Circles() As Circle = {
                New Circle With {.Radius = 5, .Center = New Point With {.X = 0, .Y = 0}},
                New Circle With {.Radius = 5, .Center = New Point With {.X = 10, .Y = 10}},
                New Circle With {.Radius = 10, .Center = New Point With {.X = 10, .Y = 10}},
                New Circle With {.Radius = 12, .Center = New Point With {.X = 0, .Y = 0}}
                }

        Dim circleGroups = From c In Circles
                        Order By c.Radius
                        Group c By c.Center Into Group

        'This is not working, not complile
        Dim doubleCircleGroups1 = From g In circleGroups
                                Where g.Group.Count() = 2
                                Select Center = g.Center, ID = g.Group(0).Radius * 2, OD = g.Group(1).Radius * 2

        'This is working
        Dim doubleCircleGroups2 = From g In circleGroups
                                Where g.Group.Count() = 2
                                Select Center = g.Center, ID = g.Group.ElementAt(0).Radius * 2, OD = g.Group.ElementAt(1).Radius * 2
    End Sub
End Module
smilinger
  • 63
  • 6
  • 3
    1. `ElementAt()` is not indexing, it's a linq extension available in c#, too (answers to the question you linked state that the "indexer" you use indeed uses `ElementAt()` or `ElementAtOrDefault()`). 2. "This is not working"? If something's not working, there usually is an error message that tells you what went wrong. If we shall help you with that, it seems natural to _tell us that error message_. If there is no error message, then surely there is a result different from what you expected, so please tell us what you expected and what happened instead. – René Vogt Jan 24 '18 at 14:15
  • 2
    It's an interesting question, but I'll need a [mcve] to debug it. Can you provide one? – Heinzi Jan 24 '18 at 14:17
  • Sorry guys, after a little more test, I find that the code is actually OK, it's the problem of Resharper, it seems R# cannot understand the indexing syntax here, so report it as an error, it's a false alarm. – smilinger Jan 24 '18 at 14:59

0 Answers0