Pastebin launched a little side project called HostCabi.net, check it out ;-)Don't like ads? PRO users don't see any ads ;-)
Guest

VB.NET linq group by with anonymous types not working

By: a guest on Sep 4th, 2012  |  syntax: VB.NET  |  size: 1.48 KB  |  hits: 7  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. Module Module1
  2.  
  3.     Public Class Company
  4.         Public Property Name As String
  5.     End Class
  6.  
  7.     Public Enum Format
  8.         Style1
  9.         Style2
  10.         Style3
  11.     End Enum
  12.  
  13.     Public Class Definition
  14.         Public Property ID As Integer
  15.         Public Property Company As Company
  16.         Public Property Format As Format
  17.     End Class
  18.  
  19.     Sub Main()
  20.  
  21.         Dim defs As New List(Of Definition)()
  22.         defs.Add(New Definition() With {.ID = 1, .Company = Nothing, .Format = Format.Style1})
  23.         defs.Add(New Definition() With {.ID = 2, .Company = Nothing, .Format = Format.Style2})
  24.         defs.Add(New Definition() With {.ID = 3, .Company = Nothing, .Format = Format.Style3})
  25.  
  26.         defs.Add(New Definition() With {.ID = 4, .Company = Nothing, .Format = Format.Style1}) '-- <<< WILDCARD 1
  27.         defs.Add(New Definition() With {.ID = 4, .Company = New Company() With {.Name = "Test"}, .Format = Format.Style1}) '-- <<< WILDCARD 2
  28.  
  29.         '-- Get groups
  30.         Dim byFormat = From d In defs
  31.                        Group d By KeyedFormat = New With {Key .Format = d.Format, Key .Company = d.Company} Into Group
  32.                        Select New With
  33.                               {
  34.                                   .Company = KeyedFormat.Company,
  35.                                   .Format = KeyedFormat.Format,
  36.                                   .Definitions = Group.OrderByDescending(Function(x) x.ID)
  37.                               }
  38.  
  39.     End Sub
  40.  
  41. End Module