LINQ - Create a nested group in vb











up vote
0
down vote

favorite












I'm trying to create nested groups in a visual basic LINQ query expression as shown here in C#: https://docs.microsoft.com/en-us/dotnet/csharp/linq/create-a-nested-group:



    var queryNestedGroups =
from student in students
group student by student.Year into newGroup1
from newGroup2 in
(from student in newGroup1
group student by student.LastName)
group newGroup2 by newGroup1.Key;


This is what I have so far:



    Dim students As New List(Of Student)
'...getting students content ommitted here...
Dim queryNestedGroups As IEnumerable(Of IGrouping(Of Integer, IGrouping(Of String, Student))) =
From student In students
Group student By student.Year Into newGroup1 = Group
From newGroup2 In (From student In newGroup1 Group student By student.LastName Into Group)
Group newGroup2 By newGroup1.Key Into Group
' ^^^^^^^^^^^^^ compiler red lines here

Dim grouping As IGrouping(Of Integer, IGrouping(Of String, Student))
For Each grouping In queryNestedGroups
Console.WriteLine($"DataClass.Student Level = {grouping.Key}")
Dim grouping2 As IGrouping(Of String, Student)
For Each grouping2 In grouping
Console.WriteLine((ChrW(9) & "Names that begin with: " & grouping2.Key))
Dim student As Student
For Each student In grouping2
Console.WriteLine((ChrW(9) & ChrW(9) & student.LastName & " " & student.FirstName))
Next
Next
Next


But the compiler is complaining about the newGroup1.Key:



"Key" is not a member of "IEnumerable(Of Student)"


What is the Visual Basic equivalent of the C# code?










share|improve this question






















  • newGroup1.Key is a Range Variable, here. It comes from an Enumerator. You need to use the direct reference: Year, as specified in Group student By student.Year.
    – Jimi
    Nov 8 at 13:53












  • You should probably add: Order By student.Year after From student In students
    – Jimi
    Nov 8 at 14:00










  • Ahm, the stupid tablet deleted a piece of the first comment. Before You need to there was: newGroup1 is an Enumerable(Of student) at this point, it has no Key.
    – Jimi
    Nov 8 at 14:23















up vote
0
down vote

favorite












I'm trying to create nested groups in a visual basic LINQ query expression as shown here in C#: https://docs.microsoft.com/en-us/dotnet/csharp/linq/create-a-nested-group:



    var queryNestedGroups =
from student in students
group student by student.Year into newGroup1
from newGroup2 in
(from student in newGroup1
group student by student.LastName)
group newGroup2 by newGroup1.Key;


This is what I have so far:



    Dim students As New List(Of Student)
'...getting students content ommitted here...
Dim queryNestedGroups As IEnumerable(Of IGrouping(Of Integer, IGrouping(Of String, Student))) =
From student In students
Group student By student.Year Into newGroup1 = Group
From newGroup2 In (From student In newGroup1 Group student By student.LastName Into Group)
Group newGroup2 By newGroup1.Key Into Group
' ^^^^^^^^^^^^^ compiler red lines here

Dim grouping As IGrouping(Of Integer, IGrouping(Of String, Student))
For Each grouping In queryNestedGroups
Console.WriteLine($"DataClass.Student Level = {grouping.Key}")
Dim grouping2 As IGrouping(Of String, Student)
For Each grouping2 In grouping
Console.WriteLine((ChrW(9) & "Names that begin with: " & grouping2.Key))
Dim student As Student
For Each student In grouping2
Console.WriteLine((ChrW(9) & ChrW(9) & student.LastName & " " & student.FirstName))
Next
Next
Next


But the compiler is complaining about the newGroup1.Key:



"Key" is not a member of "IEnumerable(Of Student)"


What is the Visual Basic equivalent of the C# code?










share|improve this question






















  • newGroup1.Key is a Range Variable, here. It comes from an Enumerator. You need to use the direct reference: Year, as specified in Group student By student.Year.
    – Jimi
    Nov 8 at 13:53












  • You should probably add: Order By student.Year after From student In students
    – Jimi
    Nov 8 at 14:00










  • Ahm, the stupid tablet deleted a piece of the first comment. Before You need to there was: newGroup1 is an Enumerable(Of student) at this point, it has no Key.
    – Jimi
    Nov 8 at 14:23













up vote
0
down vote

favorite









up vote
0
down vote

favorite











I'm trying to create nested groups in a visual basic LINQ query expression as shown here in C#: https://docs.microsoft.com/en-us/dotnet/csharp/linq/create-a-nested-group:



    var queryNestedGroups =
from student in students
group student by student.Year into newGroup1
from newGroup2 in
(from student in newGroup1
group student by student.LastName)
group newGroup2 by newGroup1.Key;


This is what I have so far:



    Dim students As New List(Of Student)
'...getting students content ommitted here...
Dim queryNestedGroups As IEnumerable(Of IGrouping(Of Integer, IGrouping(Of String, Student))) =
From student In students
Group student By student.Year Into newGroup1 = Group
From newGroup2 In (From student In newGroup1 Group student By student.LastName Into Group)
Group newGroup2 By newGroup1.Key Into Group
' ^^^^^^^^^^^^^ compiler red lines here

Dim grouping As IGrouping(Of Integer, IGrouping(Of String, Student))
For Each grouping In queryNestedGroups
Console.WriteLine($"DataClass.Student Level = {grouping.Key}")
Dim grouping2 As IGrouping(Of String, Student)
For Each grouping2 In grouping
Console.WriteLine((ChrW(9) & "Names that begin with: " & grouping2.Key))
Dim student As Student
For Each student In grouping2
Console.WriteLine((ChrW(9) & ChrW(9) & student.LastName & " " & student.FirstName))
Next
Next
Next


But the compiler is complaining about the newGroup1.Key:



"Key" is not a member of "IEnumerable(Of Student)"


What is the Visual Basic equivalent of the C# code?










share|improve this question













I'm trying to create nested groups in a visual basic LINQ query expression as shown here in C#: https://docs.microsoft.com/en-us/dotnet/csharp/linq/create-a-nested-group:



    var queryNestedGroups =
from student in students
group student by student.Year into newGroup1
from newGroup2 in
(from student in newGroup1
group student by student.LastName)
group newGroup2 by newGroup1.Key;


This is what I have so far:



    Dim students As New List(Of Student)
'...getting students content ommitted here...
Dim queryNestedGroups As IEnumerable(Of IGrouping(Of Integer, IGrouping(Of String, Student))) =
From student In students
Group student By student.Year Into newGroup1 = Group
From newGroup2 In (From student In newGroup1 Group student By student.LastName Into Group)
Group newGroup2 By newGroup1.Key Into Group
' ^^^^^^^^^^^^^ compiler red lines here

Dim grouping As IGrouping(Of Integer, IGrouping(Of String, Student))
For Each grouping In queryNestedGroups
Console.WriteLine($"DataClass.Student Level = {grouping.Key}")
Dim grouping2 As IGrouping(Of String, Student)
For Each grouping2 In grouping
Console.WriteLine((ChrW(9) & "Names that begin with: " & grouping2.Key))
Dim student As Student
For Each student In grouping2
Console.WriteLine((ChrW(9) & ChrW(9) & student.LastName & " " & student.FirstName))
Next
Next
Next


But the compiler is complaining about the newGroup1.Key:



"Key" is not a member of "IEnumerable(Of Student)"


What is the Visual Basic equivalent of the C# code?







vb.net linq






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 8 at 10:59









Olaf

762




762












  • newGroup1.Key is a Range Variable, here. It comes from an Enumerator. You need to use the direct reference: Year, as specified in Group student By student.Year.
    – Jimi
    Nov 8 at 13:53












  • You should probably add: Order By student.Year after From student In students
    – Jimi
    Nov 8 at 14:00










  • Ahm, the stupid tablet deleted a piece of the first comment. Before You need to there was: newGroup1 is an Enumerable(Of student) at this point, it has no Key.
    – Jimi
    Nov 8 at 14:23


















  • newGroup1.Key is a Range Variable, here. It comes from an Enumerator. You need to use the direct reference: Year, as specified in Group student By student.Year.
    – Jimi
    Nov 8 at 13:53












  • You should probably add: Order By student.Year after From student In students
    – Jimi
    Nov 8 at 14:00










  • Ahm, the stupid tablet deleted a piece of the first comment. Before You need to there was: newGroup1 is an Enumerable(Of student) at this point, it has no Key.
    – Jimi
    Nov 8 at 14:23
















newGroup1.Key is a Range Variable, here. It comes from an Enumerator. You need to use the direct reference: Year, as specified in Group student By student.Year.
– Jimi
Nov 8 at 13:53






newGroup1.Key is a Range Variable, here. It comes from an Enumerator. You need to use the direct reference: Year, as specified in Group student By student.Year.
– Jimi
Nov 8 at 13:53














You should probably add: Order By student.Year after From student In students
– Jimi
Nov 8 at 14:00




You should probably add: Order By student.Year after From student In students
– Jimi
Nov 8 at 14:00












Ahm, the stupid tablet deleted a piece of the first comment. Before You need to there was: newGroup1 is an Enumerable(Of student) at this point, it has no Key.
– Jimi
Nov 8 at 14:23




Ahm, the stupid tablet deleted a piece of the first comment. Before You need to there was: newGroup1 is an Enumerable(Of student) at this point, it has no Key.
– Jimi
Nov 8 at 14:23












1 Answer
1






active

oldest

votes

















up vote
1
down vote













Thanks to Jimi, this produces the same result as the C# code:



    Dim queryNestedGroups =
From student In students
Group student By student.Year Into newGroup1 = Group
From newGroup2 In (From student In newGroup1 Group student By LastName = student.LastName Into Group)
Group newGroup2 By Year Into Group

For Each grouping In queryNestedGroups
Console.WriteLine($"DataClass.Student Level = {grouping.Year}")
For Each grouping2 In grouping.Group
Console.WriteLine((ChrW(9) & "Names that begin with: " & grouping2.LastName))
Dim student As Student
For Each student In grouping2.Group
Console.WriteLine((ChrW(9) & ChrW(9) & student.LastName & " " & student.FirstName))
Next
Next
Next


As a drawback the result queryNestedGroups is now an anonymous type and no



IEnumerable(Of IGrouping(Of Integer, IGrouping(Of String, Student)))


but at least it works!






share|improve this answer





















  • The result would be IEnumerable(Of IGrouping(Of [Enum], IGrouping(Of String, Student))). You can make the the anonymous type re-appear as a Grouping with: Dim StudentGrouping = [Students].GroupBy(Function(student) student.Year).GroupBy(Function(group) group.Key).ToList()
    – Jimi
    Nov 8 at 15:56












  • No, that's just a List(Of IGrouping(Of GradeLevel, IGrouping(Of GradeLevel, Student))) - the String is missing...
    – Olaf
    Nov 9 at 8:28













Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














 

draft saved


draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53206361%2flinq-create-a-nested-group-in-vb%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes








up vote
1
down vote













Thanks to Jimi, this produces the same result as the C# code:



    Dim queryNestedGroups =
From student In students
Group student By student.Year Into newGroup1 = Group
From newGroup2 In (From student In newGroup1 Group student By LastName = student.LastName Into Group)
Group newGroup2 By Year Into Group

For Each grouping In queryNestedGroups
Console.WriteLine($"DataClass.Student Level = {grouping.Year}")
For Each grouping2 In grouping.Group
Console.WriteLine((ChrW(9) & "Names that begin with: " & grouping2.LastName))
Dim student As Student
For Each student In grouping2.Group
Console.WriteLine((ChrW(9) & ChrW(9) & student.LastName & " " & student.FirstName))
Next
Next
Next


As a drawback the result queryNestedGroups is now an anonymous type and no



IEnumerable(Of IGrouping(Of Integer, IGrouping(Of String, Student)))


but at least it works!






share|improve this answer





















  • The result would be IEnumerable(Of IGrouping(Of [Enum], IGrouping(Of String, Student))). You can make the the anonymous type re-appear as a Grouping with: Dim StudentGrouping = [Students].GroupBy(Function(student) student.Year).GroupBy(Function(group) group.Key).ToList()
    – Jimi
    Nov 8 at 15:56












  • No, that's just a List(Of IGrouping(Of GradeLevel, IGrouping(Of GradeLevel, Student))) - the String is missing...
    – Olaf
    Nov 9 at 8:28

















up vote
1
down vote













Thanks to Jimi, this produces the same result as the C# code:



    Dim queryNestedGroups =
From student In students
Group student By student.Year Into newGroup1 = Group
From newGroup2 In (From student In newGroup1 Group student By LastName = student.LastName Into Group)
Group newGroup2 By Year Into Group

For Each grouping In queryNestedGroups
Console.WriteLine($"DataClass.Student Level = {grouping.Year}")
For Each grouping2 In grouping.Group
Console.WriteLine((ChrW(9) & "Names that begin with: " & grouping2.LastName))
Dim student As Student
For Each student In grouping2.Group
Console.WriteLine((ChrW(9) & ChrW(9) & student.LastName & " " & student.FirstName))
Next
Next
Next


As a drawback the result queryNestedGroups is now an anonymous type and no



IEnumerable(Of IGrouping(Of Integer, IGrouping(Of String, Student)))


but at least it works!






share|improve this answer





















  • The result would be IEnumerable(Of IGrouping(Of [Enum], IGrouping(Of String, Student))). You can make the the anonymous type re-appear as a Grouping with: Dim StudentGrouping = [Students].GroupBy(Function(student) student.Year).GroupBy(Function(group) group.Key).ToList()
    – Jimi
    Nov 8 at 15:56












  • No, that's just a List(Of IGrouping(Of GradeLevel, IGrouping(Of GradeLevel, Student))) - the String is missing...
    – Olaf
    Nov 9 at 8:28















up vote
1
down vote










up vote
1
down vote









Thanks to Jimi, this produces the same result as the C# code:



    Dim queryNestedGroups =
From student In students
Group student By student.Year Into newGroup1 = Group
From newGroup2 In (From student In newGroup1 Group student By LastName = student.LastName Into Group)
Group newGroup2 By Year Into Group

For Each grouping In queryNestedGroups
Console.WriteLine($"DataClass.Student Level = {grouping.Year}")
For Each grouping2 In grouping.Group
Console.WriteLine((ChrW(9) & "Names that begin with: " & grouping2.LastName))
Dim student As Student
For Each student In grouping2.Group
Console.WriteLine((ChrW(9) & ChrW(9) & student.LastName & " " & student.FirstName))
Next
Next
Next


As a drawback the result queryNestedGroups is now an anonymous type and no



IEnumerable(Of IGrouping(Of Integer, IGrouping(Of String, Student)))


but at least it works!






share|improve this answer












Thanks to Jimi, this produces the same result as the C# code:



    Dim queryNestedGroups =
From student In students
Group student By student.Year Into newGroup1 = Group
From newGroup2 In (From student In newGroup1 Group student By LastName = student.LastName Into Group)
Group newGroup2 By Year Into Group

For Each grouping In queryNestedGroups
Console.WriteLine($"DataClass.Student Level = {grouping.Year}")
For Each grouping2 In grouping.Group
Console.WriteLine((ChrW(9) & "Names that begin with: " & grouping2.LastName))
Dim student As Student
For Each student In grouping2.Group
Console.WriteLine((ChrW(9) & ChrW(9) & student.LastName & " " & student.FirstName))
Next
Next
Next


As a drawback the result queryNestedGroups is now an anonymous type and no



IEnumerable(Of IGrouping(Of Integer, IGrouping(Of String, Student)))


but at least it works!







share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 8 at 14:48









Olaf

762




762












  • The result would be IEnumerable(Of IGrouping(Of [Enum], IGrouping(Of String, Student))). You can make the the anonymous type re-appear as a Grouping with: Dim StudentGrouping = [Students].GroupBy(Function(student) student.Year).GroupBy(Function(group) group.Key).ToList()
    – Jimi
    Nov 8 at 15:56












  • No, that's just a List(Of IGrouping(Of GradeLevel, IGrouping(Of GradeLevel, Student))) - the String is missing...
    – Olaf
    Nov 9 at 8:28




















  • The result would be IEnumerable(Of IGrouping(Of [Enum], IGrouping(Of String, Student))). You can make the the anonymous type re-appear as a Grouping with: Dim StudentGrouping = [Students].GroupBy(Function(student) student.Year).GroupBy(Function(group) group.Key).ToList()
    – Jimi
    Nov 8 at 15:56












  • No, that's just a List(Of IGrouping(Of GradeLevel, IGrouping(Of GradeLevel, Student))) - the String is missing...
    – Olaf
    Nov 9 at 8:28


















The result would be IEnumerable(Of IGrouping(Of [Enum], IGrouping(Of String, Student))). You can make the the anonymous type re-appear as a Grouping with: Dim StudentGrouping = [Students].GroupBy(Function(student) student.Year).GroupBy(Function(group) group.Key).ToList()
– Jimi
Nov 8 at 15:56






The result would be IEnumerable(Of IGrouping(Of [Enum], IGrouping(Of String, Student))). You can make the the anonymous type re-appear as a Grouping with: Dim StudentGrouping = [Students].GroupBy(Function(student) student.Year).GroupBy(Function(group) group.Key).ToList()
– Jimi
Nov 8 at 15:56














No, that's just a List(Of IGrouping(Of GradeLevel, IGrouping(Of GradeLevel, Student))) - the String is missing...
– Olaf
Nov 9 at 8:28






No, that's just a List(Of IGrouping(Of GradeLevel, IGrouping(Of GradeLevel, Student))) - the String is missing...
– Olaf
Nov 9 at 8:28




















 

draft saved


draft discarded



















































 


draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53206361%2flinq-create-a-nested-group-in-vb%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Schultheiß

Liste der Kulturdenkmale in Wilsdruff

Android Play Services Check