Quote:
have to calculate the total score and then output it with the corresponding student name
If I read and understood your question correctly, This is what you're looking for:
class Student
{
public string Name { get; set; }
public float Score { get; set; }
}
private static void Main()
{
var List1 = new List<Student>
{
new Student { Name="Student 1", Score = 78.1f },
new Student { Name="Student 2", Score = 88f }
};
var List2 = new List<Student>
{
new Student { Name="Student 3", Score = 52.7f },
new Student { Name="Student 1", Score = 48f }
};
var results = List1.Concat(List2)
.GroupBy(x => x.Name)
.Select(x => new Student
{
Name = x.Key,
Score = x.Sum(y => y.Score)
});
foreach (var student in results)
{
Console.WriteLine($"Student: {student.Name} > Score: {student.Score:N2}");
}
}
Which outputs:
Student: Student 1 > Score: 126.10
Student: Student 2 > Score: 88.00
Student: Student 3 > Score: 52.70
UPDATE: My bad, Here is the VB.NET version:
Class Student
Public Property Name() As String
Public Property Score() As Single
End Class
Module Module1
Sub Main()
Dim List1 = New List(Of Student)() From {
New Student() With {
.Name = "Student 1",
.Score = 78.1F
},
New Student() With {
.Name = "Student 2",
.Score = 88.0F
}
}
Dim List2 = New List(Of Student)() From {
New Student() With {
.Name = "Student 3",
.Score = 52.7F
},
New Student() With {
.Name = "Student 1",
.Score = 48.0F
}
}
Dim results = List1.Concat(List2) _
.GroupBy(Function(x) x.Name) _
.Select(Function(x) New Student() With {
.Name = x.Key,
.Score = x.Sum(Function(y) y.Score)
})
For Each student In results
Console.WriteLine("Student: {student.Name} > Score: {student.Score:N2}")
Next
End Sub
End Module