Reputation: 1
I have a class with these properties:
public string FirstName { get; set; }
public string LastName { get; set; }
public int ID { get; set; }
public GradeLevel Year;
public List<int> ExamScores;
And a method:
protected static List<Student> students = new List<Student>
{
new Student {FirstName = "Terry", LastName = "Adams", ID = 120,
Year = GradeLevel.SecondYear,
ExamScores = new List<int>{ 99, 82, 81, 92}},
new Student {FirstName = "Fadi", LastName = "Fakhouri", ID = 116,
Year = GradeLevel.ThirdYear,
ExamScores = new List<int>{ 99, 81, 90, 94}},
new Student {FirstName = "Hanying", LastName = "Feng", ID = 117,
Year = GradeLevel.FirstYear,
ExamScores = new List<int>{ 90, 94, 81, 82}},
}
Next step:
public void GroupByMark()
{
students.ForEach(mark =>
{
var queryNumericRange =
from student in students
group new {student.FirstName, student.LastName} by mark
into percentGroup
orderby percentGroup.Key
select percentGroup;
foreach (var studentGroup in queryNumericRange)
{
Console.WriteLine("Key: {0}", (studentGroup.Key));
foreach (var item in studentGroup)
{
Console.WriteLine("\t{0}, {1}", item.LastName, item.FirstName);
}
}
});
}
Result:
key: test.StudentClass+Student
Adams, Terry
Fakhouri, Fadi
Feng, Hanying
key: test.StudentClass+Student
Adams, Terry
Fakhouri, Fadi
Feng, Hanying
key: test.StudentClass+Student
Adams, Terry
Fakhouri, Fadi
Feng, Hanying
Desired result:
key: 99
Adams, Terry
Fakhouri, Fadi
key: 82
Adams, Terry
Feng, Hanying
key: 81
Adams, Terry
Fakhouri, Fadi
key: 92
Adams, Terry
key: 90
Fakhouri, Fadi
Feng, Hanying
key: 94
Fakhouri, Fadi
Feng, Hanying
So, for each mark I would like to display list of students, who received that particular mark. Do you have any ideas how to do it?
Upvotes: 0
Views: 276
Reputation: 1499800
It sounds like you want to create a sequence of student/mark pairs, and then group by the mark:
var studentsByMark = from student in students
from mark in student.ExamScores
group student by mark;
foreach (var group in studentsByMark)
{
Console.WriteLine("{0}:", group.Key); // Mark
foreach (var student in group)
{
Console.WriteLine("{0}, {1}", student.LastName, student.FirstName);
}
}
Upvotes: 0
Reputation: 27861
Here is how you can do it:
var results = students
.SelectMany(score => score.ExamScores) //get all scores from all students
.Distinct() //remove duplicate scores
.Select(score => //for each score create a new anonymous object
new
{
Score = score, //That contains the score itself
Students = students //And the students that have such score
.Where(student => student.ExamScores.Contains(score)).ToList()
})
.ToList();
Or like this if you just care about the student names:
var results = students
.SelectMany(score => score.ExamScores)
.Distinct()
.Select(score =>
new
{
Score = score,
Students = students
.Where(student => student.ExamScores.Contains(score))
.Select(student => student.LastName + ", " + student.FirstName)
.ToList()
})
.ToList();
Here is how you can loop through the results in the later case:
foreach (var result in results)
{
Console.WriteLine("Score: " + result.Score);
foreach (var student in result.Students)
{
Console.WriteLine(student);
}
}
Upvotes: 0
Reputation: 21795
Yop can use SelectMany
to flatten the list and then group by exam scores like this:-
var queryNumericRange = students.SelectMany(x => x.ExamScores, (stuObj, scores) => new
{
StudentName = String.Format("{0},{1}",stuObj.LastName,stuObj.FirstName),
ExamScores = scores
})
.GroupBy(x => x.ExamScores)
.Select(x => new
{
Key = x.Key,
StudentNames = x.Select(s => s.StudentName).ToList()
});
Upvotes: 1