C# 这个lambda表达式能变得更简单吗? var malecont=students.Where(Std=>Std.Gender.ToUpper()==“M”).Count(); var femalecont=students.Where(Std=>Std.Gender.ToUpper()==“F”).Count(); //存储优秀学生记录的列表 List TopStudents=新列表(); //将记录添加到列表中 如果(MaleCount>0) { var maxMarksM=students.Where(o=>o.Gender.ToUpper()==“M”).Max(o=>o.Marks); topstudens=students.Where(o=>o.Gender.ToUpper()==“M”&o.Marks==maxMarksM.ToList(); } 如果(女性人数>0) { var maxMarksF=students.Where(o=>o.Gender.ToUpper()==“F”).Max(o=>o.Marks); AddRange(students.Where(o=>o.Gender.ToUpper()==“F”和&o.Marks==maxMarksF.ToList()); } 返回优秀学生;

C# 这个lambda表达式能变得更简单吗? var malecont=students.Where(Std=>Std.Gender.ToUpper()==“M”).Count(); var femalecont=students.Where(Std=>Std.Gender.ToUpper()==“F”).Count(); //存储优秀学生记录的列表 List TopStudents=新列表(); //将记录添加到列表中 如果(MaleCount>0) { var maxMarksM=students.Where(o=>o.Gender.ToUpper()==“M”).Max(o=>o.Marks); topstudens=students.Where(o=>o.Gender.ToUpper()==“M”&o.Marks==maxMarksM.ToList(); } 如果(女性人数>0) { var maxMarksF=students.Where(o=>o.Gender.ToUpper()==“F”).Max(o=>o.Marks); AddRange(students.Where(o=>o.Gender.ToUpper()==“F”和&o.Marks==maxMarksF.ToList()); } 返回优秀学生;,c#,lambda,C#,Lambda,编辑: @Alexey Subbota建议g.Max可能会被调用太多次,事实上,小组内的每个学生都会被调用一次,这是不必要的,我们只需要为每个小组计算一次最大值。 如果是问题,您可以这样做: var topStudents = allStudents .GroupBy(s => s.Gender.ToUpper()) // Dividing the students to groups by gender .Where(g =

编辑:

@Alexey Subbota建议g.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();
var topstudens=所有学生
.GroupBy(s=>s.Gender.ToUpper())//将学生按性别分组
其中(g=>g.Key==“M”| | g.Key==“F”)//仅包括男性和女性
.Select(g=>newkeyvaluepair(g.Max(s=>s.Marks),g))//将组与其最高得分值一起存储
。从每组中选择(g=>g.Value.Where(s=>s.Marks==g.Key))//选择得分最高的学生
.SelectMany(s=>s)//从所有内部组中选择所有学生
.ToList();
编辑:

@Alexey Subbota建议g.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();
var topstudens=所有学生
.GroupBy(s=>s.Gender.ToUpper())//将学生按性别分组
其中(g=>g.Key==“M”| | g.Key==“F”)//仅包括男性和女性
.Select(g=>newkeyvaluepair(g.Max(s=>s.Marks),g))//将组与其最高得分值一起存储
。从每组中选择(g=>g.Value.Where(s=>s.Marks==g.Key))//选择得分最高的学生
.SelectMany(s=>s)//从所有内部组中选择所有学生
.ToList();

免责声明:我在Groovy中编写了一个程序,但在这种情况下这不会有什么区别

如果您不想使用chained
.GroupBy()。Where()…
解决方案,可以这样做:

学生
分为
男性学生
女性学生
(使用
Where()

这将消除对if包装的需要,也不需要在男女学生的行中使用
Count()

因此,我的备选方案应该如下所示:

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();
var-MaleStudents=students.Where(Std=>Std.Gender.ToUpper()==“M”);
var FemaleStudents=students.Where(Std=>Std.Gender.ToUpper()==“F”);
//存储优秀学生记录的列表
List TopStudents=新列表();
//将记录添加到列表中
var maxMarksM=MaleStudents.Max(o=>o.Marks);
topstudens=malestudens.Where(o=>o.Marks==maxMarksM.ToList();
var maxMarksF=FemaleStudents.Max(o=>o.Marks);
AddRange(femalestudent.Where(o=>o.Marks==maxMarksF.ToList());
返回优秀学生;

免责声明:我在Groovy中编写了一个程序,但在这种情况下这不会有什么区别

如果您不想使用chained
.GroupBy()。Where()…
解决方案,可以这样做:

学生
分为
男性学生
女性学生
(使用
Where()

这将消除对if包装的需要,也不需要在男女学生的行中使用
Count()

因此,我的备选方案应该如下所示:

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();
var-MaleStudents=students.Where(Std=>Std.Gender.ToUpper()==“M”);
var FemaleStudents=students.Where(Std=>Std.Gender.ToUpper()==“F”);
//存储优秀学生记录的列表
List TopStudents=新列表();
//将记录添加到列表中
var maxMarksM=MaleStudents.Max(o=>o.Marks);
topstudens=malestudens.Where(o=>o.Marks==maxMarksM.ToList();
var maxMarksF=FemaleStudents.Max(o=>o.Marks);
AddRange(femalestudent.Where(o=>o.Marks==maxMarksF.ToList());
返回优秀学生;

至少您可以避免使用计数。。。(男性计数和女性计数)…至少你可以不使用计数。。。(MaleCount和FemaleCount)…答案不错,但对我来说,很难称之为“更简单”^^我担心g.Max(I=>I.Marks)会被称为太多了times@AlexeySubbota是的,你说得对。我已经相应地编辑了我的答案。@Spawn如果你没有做过很多“函数式编程”的话,可能会有点难理解,但就大小而言,它要短得多,也更容易修改,你可以很容易地扩展它以拥有更多的组,您可以轻松地向查询添加更多过滤器,甚至可以毫不费力地使其成为多线程和并行的(通过使用AsParallel方法)。Linq是一个非常强大的工具,我相信一旦你学会了如何正确使用它,就很难回头了。答案很好,但对我来说,很难称之为“更简单”^我担心g.Max(i=>i.Marks)会被称为太多times@AlexeySubbota是的,你说得对。我已经相应地编辑了我的答案。@Spawn如果你没有做过很多“函数式编程”的话,它可能会更难理解,但就大小而言,它要短得多,也更容易修改,你可以很容易地扩展它以拥有更多的组,你可以很容易地在查询中添加更多的过滤器,甚至可以