-4

I have the following two methods

/// <summary>
/// Reports the number of students in each ClassLevel designation
/// </summary>
/// <param name="students">The original collection of students</param>
/// <returns>A Dictionary where the key is the ClassLevel and the value is the number of students in that level</returns>
public static Dictionary<ClassLevel, int> StudentsPerClassLevel(this IEnumerable<Student> students)
{
     var query = students.GroupBy(student => student.Level).ToDictionary(x => x.Key, x => x.Count());
     return query;
}

/// <summary>
/// Determines which MaritalStatus has the highest average GPA
/// </summary>
/// <param name="students">The original collection of students</param>
/// <returns>The MaritalStatus value with the highest average GPA</returns>
public static MaritalStatus MaritalStatusWithHighestAverageGPA(this IEnumerable<Student> students)
{
        var query = students.GroupBy(s => s.Relationship).ToDictionary(x => x.Key, x => x.Average(g => g.GPA)).OrderByDescending(d => d.Value).First().Key;

        return query;
}

None of these methods work, I am trying to figure out where I went wrong and what I need to do in order to get what I am trying to achieve.

Any suggestions?

Technocrat
  • 314
  • 1
  • 3
  • 15
  • 1
    In what way do they not work? And what is it you're trying to achieve? – Jeroen Vannevel Feb 07 '15 at 21:53
  • Your first method requires a `GroupBy`, the second one requires a Max function. I'll need to look at those methods a little more to give you more help. – Michael Welch Feb 07 '15 at 21:55
  • @JeroenVannevel I had the same comment at first, but then if you look at the XML docs you can see what is desired. – Michael Welch Feb 07 '15 at 22:07
  • "None of these methods work". I don't believe that. That means: they do nothing. You probably mean they don't work as expected, but then you should elaborate on the expectation and the actual result. – Gert Arnold Feb 07 '15 at 22:24
  • Gert, I said they do not work because they wont compile. The desired return value is not what the method signature expects to be returned. On the first one it works, not on the second one. – Technocrat Feb 08 '15 at 00:41

1 Answers1

1

The first method should use this method: https://msdn.microsoft.com/en-us/library/vstudio/bb549393(v=vs.100).aspx

Something like:

return students.GroupBy(student => student.Level, (level, students) => new Tuple(level, students.Count())); 

Sorry I'm unable to test this, but basically like your version I'm using a lambda to say Group all students by level. The second lambda tells the method what to do with those groups. In this instance, for each group, I'm creating a tuple with the level and the count of students in each group.

Your return value should be changed to IEnumerable<Tuple<ClassLevel, int>> which could easily be converted to a Dictionary if you wished.

I can't guess at the syntax for solving your second method without a compiler. But this questions uses Max: LINQ Using Max() to select a single row

Edit This version appears to at least compile. Hopefully, you arrived at something similar

    public static Dictionary<ClassLevel, int> StudentsPerClassLevel(this IEnumerable<Student> students)
    {
        return students.GroupBy(student => student.ClassLevel, 
            (level, s) => new Tuple<ClassLevel, int>(level, s.Count())).ToDictionary(t => t.Item1, t => t.Item2);

    }
Community
  • 1
  • 1
Michael Welch
  • 1,732
  • 2
  • 18
  • 29
  • Your answer is close, ClassLevel is an Enum so somewhere I have to compare against the Enums of ClassLevel.Sophomore, ClassLevel.Junior, ClassLevel.Freshman, or ClassLevel.Senior. It's also saying "Cannot convert lambda expression to type Collections.Generic.IQqualityComparer because it is not a delegate type – Technocrat Feb 08 '15 at 19:16
  • var query = students.GroupBy(student => student.Level, (ClassLevel) => new Tuple(ClassLevel, students.Count())); It's throwing an error whenever I put anything after ClassLevel, as in your example (level, students) It's asking for a comparator, I thought it would work – Technocrat Feb 08 '15 at 19:17
  • Sorry about that. I have access to Visual Studio now to work this out in more detail. Surprised no one else answered this already. – Michael Welch Feb 08 '15 at 19:37
  • I figured it out! Thank you, I will update my answer – Technocrat Feb 08 '15 at 19:39