0

I have spent hours looking through the archives here and on the web on how to iterate an IGroup that has multiple groupings for VB.Net. There are quite a few solutions for C# but I am really struggling to convert to VB.Net.

Here is my List that I am grouping

dim merge_list As List(Of MergeData)

Here is my code to group by three properties in the List

 Dim groups = merge_list.GroupBy(Function(t) New With {Key t.GUID.Category,
                                       Key t.GUID.FeatureType,
                                       Key t.GUID.AssetType}) _
                                       .Where(Function(grp) grp.Count > 1)

Next I try to loop through the groups but since I don't have a type (i.e. dim group As SomeType) for the items in the groups list I don't know how to work with them.

Here is my code to loop through the groups

dim group '===note there is no "As SomeType" for group just 'Dim group'===
For Each group In groups
  ' since group is not typed I cannot figure out how to work with it 

next

Can someone please let me know how to iterate an IGoup with multiple groups using VB.NET?

scott_f
  • 838
  • 8
  • 17
  • You don't need to declare `group` before `For Each`. This way compiler should figure out what the type of item in `groups` is. – MarcinJuraszek Dec 30 '14 at 01:41
  • This question was down voted. How could I improve it? – scott_f Dec 30 '14 at 01:41
  • Yes I do need to declare group as I am using VB, if I don't I get a compiler error: "'group' is not declared. It may be inaccessible due to its protection level". I prefer C# where you don't need to do this. VB is frustrating. – scott_f Dec 30 '14 at 01:44
  • Well, I just tried it and you don't. Check my answer. – MarcinJuraszek Dec 30 '14 at 01:48
  • @scott_f - Nope in VB too there is no need to specify the type. Check my answer and Fiddle attached to it. – Rahul Singh Dec 30 '14 at 02:23

2 Answers2

2

You don't have to declare group outside of For Each statement. Check following code out:

Dim source = Enumerable.Range(0, 1000)

Dim groups = source.GroupBy(Function(x) New With {
                                Key .ByFive = x Mod 5,
                                Key .ByTen = x Mod 10,
                                Key .ByTwenty = x Mod 20})

For Each group In groups
    Console.WriteLine("ByFive: {0}, ByTen: {1}, ByTwenty: {2}, Count: {3}",
                  group.Key.ByFive, group.Key.ByTen, group.Key.ByTen, group.Count())
Next

It compiles just fine and prints:

ByFive: 0, ByTen: 0, ByTwenty: 0, Count: 50
ByFive: 1, ByTen: 1, ByTwenty: 1, Count: 50
ByFive: 2, ByTen: 2, ByTwenty: 2, Count: 50
ByFive: 3, ByTen: 3, ByTwenty: 3, Count: 50
ByFive: 4, ByTen: 4, ByTwenty: 4, Count: 50
ByFive: 0, ByTen: 5, ByTwenty: 5, Count: 50
ByFive: 1, ByTen: 6, ByTwenty: 6, Count: 50
ByFive: 2, ByTen: 7, ByTwenty: 7, Count: 50
ByFive: 3, ByTen: 8, ByTwenty: 8, Count: 50
ByFive: 4, ByTen: 9, ByTwenty: 9, Count: 50
ByFive: 0, ByTen: 0, ByTwenty: 10, Count: 50
ByFive: 1, ByTen: 1, ByTwenty: 11, Count: 50
ByFive: 2, ByTen: 2, ByTwenty: 12, Count: 50
ByFive: 3, ByTen: 3, ByTwenty: 13, Count: 50
ByFive: 4, ByTen: 4, ByTwenty: 14, Count: 50
ByFive: 0, ByTen: 5, ByTwenty: 15, Count: 50
ByFive: 1, ByTen: 6, ByTwenty: 16, Count: 50
ByFive: 2, ByTen: 7, ByTwenty: 17, Count: 50
ByFive: 3, ByTen: 8, ByTwenty: 18, Count: 50
ByFive: 4, ByTen: 9, ByTwenty: 19, Count: 50

You even get intellisense when dealing with group inside For Each!

MarcinJuraszek
  • 124,003
  • 15
  • 196
  • 263
  • Things that make you go hummm? I get the same compiler error when using your code. I took a screen shot to prove I am not crazy: https://drive.google.com/a/slingblade.co.nz/file/d/0B-NzIeN88P9lUVRKRXZDWUpPNk0/view?usp=sharing . It must be a setting in Visual Studio? I will search and see what I can find. – scott_f Dec 30 '14 at 02:03
  • I had to manually add the Option Strict Off and Option Infer On to my class, even though both flags are set in the Tools>Options>Project and Settings>VB Defaults settings area. Once I did this then I was able to use intellisense. Thanks for the help! – scott_f Dec 30 '14 at 02:30
  • Someone down voted my question (he posted a horrible answer [mix of C# and my VB.Net code with no words, code only] and I politely pointed out the issues with it and he then deleted the answer and down voted my question). If you think its an ok question can you up vote it please? I an trying to earn reputation points and that hurt. – scott_f Dec 30 '14 at 03:25
  • You should never ask people to upvote your questions. You can earn reputation points by asking good questions, editing other, etc. Just be patient. Asking for upvotes is not the right way. – MarcinJuraszek Dec 30 '14 at 03:40
2

Simply use a nested For Each like this:-

 For Each item In groups
        Console.WriteLine(item.Key.GUID.Category) //You can access only `Keys` here
        For Each inneritem In item
              Console.WriteLine(inneritem.GUID.Category)
               //so on (You can access all the properties here)
        Next
 Next

Check this Fiddle.

Rahul Singh
  • 21,585
  • 6
  • 41
  • 56