var MaleCount = students.Where(Std => Std.Gender.ToUpper() == "M").Count();
var FemaleCount = students.Where(Std => Std.Gender.ToUpper() == "F").Count();
//List for storing top students records
List<StudentEntity> TopStudents = new List<StudentEntity>();
//Adding records to List
if (MaleCount > 0)
{
var maxMarksM = students.Where(o => o.Gender.ToUpper() == "M").Max(o => o.Marks);
TopStudents = students.Where(o => o.Gender.ToUpper() == "M" && o.Marks == maxMarksM).ToList();
}
if (FemaleCount > 0)
{
var maxMarksF = students.Where(o => o.Gender.ToUpper() == "F").Max(o => o.Marks);
TopStudents.AddRange(students.Where(o => o.Gender.ToUpper() == "F" && o.Marks == maxMarksF).ToList());
}
return TopStudents;
回答
免责声明:我在Groovy编程,但是这不应该在这种情况下的差异。
如果你不想这样,也许使用链式.GroupBy().Where()...
解决方案:
拆分students
到maleStudents
和femaleStudents
(使用Where()
)。
这应该消除了围绕男性和女性学生的if-wrappers和Count()
的需要。
所以我的另类应该是这样的:
var MaleStudents = students.Where(Std => Std.Gender.ToUpper() == "M");
var FemaleStudents = students.Where(Std => Std.Gender.ToUpper() == "F");
//List for storing top students records
List<StudentEntity> TopStudents = new List<StudentEntity>();
//Adding records to List
var maxMarksM = MaleStudents.Max(o => o.Marks);
TopStudents = MaleStudents.Where(o => o.Marks == maxMarksM).ToList();
var maxMarksF = FemaleStudents.Max(o => o.Marks);
TopStudents.AddRange(FemaleStudents.Where(o => o.Marks == maxMarksF).ToList());
return TopStudents;
我觉得比上次'.ToList()'没有必要。 –
我认为,在前两个'Where'之后,首选写'.ToList()'。 –
最重要的是,如果MaleStudents为空,那么'Max()'将失败。 –
var topStudents = allStudents
.GroupBy(s => s.Gender.ToUpper()) // Dividing the students to groups by gender
.Where(g => g.Key == "M" || g.Key == "F") // Including only the Male and Female genders
.Select(g => g.Where(s => s.Marks == g.Max(i => i.Marks))) // From each group, finding the highest mark and selecting from that groups all the student with that mark
.SelectMany(s => s) // selecting all the students from all the inner groups
.ToList();
编辑:
@Alexey Subbota建议,栽培大豆可能会叫的次数太多,事实上,它会被调用一次组内每一个学生,这是不必要的,只有我们需要为每个组计算一次最大值。 如果这是一个问题,你可以这样做:
var topStudents = allStudents
.GroupBy(s => s.Gender.ToUpper()) // Dividing the students to groups by gender
.Where(g => g.Key == "M" || g.Key == "F") // Including only the Male and Female genders
.Select(g => new KeyValuePair<int, IEnumerable<Student>>(g.Max(s => s.Marks), g)) // Storing the group together with it's highest score value
.Select(g => g.Value.Where(s => s.Marks == g.Key)) // From each group, selecting the student that have the highest score
.SelectMany(s => s) // selecting all the students from all the inner groups
.ToList();
- 1. 我可以用更简单的形式重写这个布尔表达式吗?
- 2. 这是lambda表达式吗?
- 3. 这个SQL约束可以被写得更简单吗?
- 4. 这个流口水的规则可以写得更简单吗?
- 5. 这个Python循环可以更简洁地表达吗?
- 6. 这个布尔表达式可以被简化吗?
- 7. 这个正则表达式可以简化吗?
- 8. 我可以做得更简单吗?
- 9. 下面可以变得更简单/更有效吗?
- 10. 从这可能是一个lambda表达式吗?
- 11. 这个正则表达式可以更有效吗?
- 12. 这个lambda是否可以重写为查询表达式
- 13. 这个循环可以写得更简洁吗?
- 14. 这个Ruby代码可以写得更简洁吗?
- 15. 可以正则表达式做得更快吗?
- 16. 有什么方法可以简化这个流表达式?
- 17. 我可以在这里使用lambda表达式(从numpy.core.records.recarray中选择记录)吗?
- 18. 变换lambda表达式
- 19. 这两个lambda表达式有区别吗?
- 20. 我可以在params关键字中使用lambda表达式吗?
- 21. 使用foreach可以简单得多吗?
- 22. 可以这样表达简化
- 23. 我可以做一个像这样的正则表达式吗?
- 24. 有人可以解释这个正则表达式吗?
- 25. 这个正则表达式可以改进吗?
- 26. 我可以调试这个正则表达式吗?
- 27. 这个正则表达式可以改进吗?
- 28. 你可以改进这个C#正则表达式代码吗?
- 29. 这个正则表达式可以优化吗?
- 30. 这些正则表达式如何写得更简洁?
至少你可以不使用计...(MaleCount和FemaleCount)... – Spawn