【发布时间】:2015-02-07 21:49:35
【问题描述】:
我有以下两种方法
/// <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;
}
这些方法都不起作用,我想弄清楚我哪里出错了,我需要做什么才能达到我想要实现的目标。
有什么建议吗?
【问题讨论】:
-
它们在哪些方面不起作用?你想达到什么目的?
-
您的第一种方法需要
GroupBy,第二种方法需要Max 函数。我需要更多地研究这些方法,以便为您提供更多帮助。 -
@JeroenVannevel 一开始我也有同样的评论,但如果你查看 XML 文档,你会发现需要什么。
-
“这些方法都不起作用”。我不相信。这意味着:他们什么都不做。您可能的意思是它们没有按预期工作,但您应该详细说明预期和实际结果。
-
Gert,我说它们不起作用,因为它们不会编译。期望的返回值不是方法签名期望返回的值。第一个有效,第二个无效。