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?
vb.net linq
add a comment |
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?
vb.net linq
newGroup1.Key
is a Range Variable, here. It comes from an Enumerator. You need to use the direct reference:Year
, as specified inGroup student By student.Year
.
– Jimi
Nov 8 at 13:53
You should probably add:Order By student.Year
afterFrom 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 anEnumerable(Of student)
at this point, it has noKey
.
– Jimi
Nov 8 at 14:23
add a comment |
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?
vb.net linq
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
vb.net linq
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 inGroup student By student.Year
.
– Jimi
Nov 8 at 13:53
You should probably add:Order By student.Year
afterFrom 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 anEnumerable(Of student)
at this point, it has noKey
.
– Jimi
Nov 8 at 14:23
add a comment |
newGroup1.Key
is a Range Variable, here. It comes from an Enumerator. You need to use the direct reference:Year
, as specified inGroup student By student.Year
.
– Jimi
Nov 8 at 13:53
You should probably add:Order By student.Year
afterFrom 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 anEnumerable(Of student)
at this point, it has noKey
.
– 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
add a comment |
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!
The result would beIEnumerable(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 aList(Of IGrouping(Of GradeLevel, IGrouping(Of GradeLevel, Student)))
- theString
is missing...
– Olaf
Nov 9 at 8:28
add a comment |
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!
The result would beIEnumerable(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 aList(Of IGrouping(Of GradeLevel, IGrouping(Of GradeLevel, Student)))
- theString
is missing...
– Olaf
Nov 9 at 8:28
add a comment |
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!
The result would beIEnumerable(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 aList(Of IGrouping(Of GradeLevel, IGrouping(Of GradeLevel, Student)))
- theString
is missing...
– Olaf
Nov 9 at 8:28
add a comment |
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!
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!
answered Nov 8 at 14:48
Olaf
762
762
The result would beIEnumerable(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 aList(Of IGrouping(Of GradeLevel, IGrouping(Of GradeLevel, Student)))
- theString
is missing...
– Olaf
Nov 9 at 8:28
add a comment |
The result would beIEnumerable(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 aList(Of IGrouping(Of GradeLevel, IGrouping(Of GradeLevel, Student)))
- theString
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
add a comment |
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
newGroup1.Key
is a Range Variable, here. It comes from an Enumerator. You need to use the direct reference:Year
, as specified inGroup student By student.Year
.– Jimi
Nov 8 at 13:53
You should probably add:
Order By student.Year
afterFrom 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 anEnumerable(Of student)
at this point, it has noKey
.– Jimi
Nov 8 at 14:23