How to use GroupBy() to group over multiple columns with VB.NET?

后端 未结 1 1896
臣服心动
臣服心动 2020-12-10 07:17

i tried to do this like i would in C#, with an anonymous type but the result is just not correct.

VB.NET example (wrong output):

Module Module1

Sub          


        
相关标签:
1条回答
  • 2020-12-10 08:21

    You need to use the Key modifier when creating the anonymous types in the VB code. By default it creates read/write properties whereas C# anonymous types are always read-only. Only read-only properties are used in Equals / GetHashCode.

    For Each item In ls.GroupBy(Function(k) New With { Key .Age = k.Age, _
                                                       Key .Sex = k.Sex})
    

    See the documentation for anonymous types in VB for more details.

    0 讨论(0)
提交回复
热议问题