在WPF中使用LINQ按列分组,实现总分、平均分及星级统计
没问题,我来帮你实现这个WPF里用LINQ分组统计成绩的需求,下面是完整的步骤和代码示例:
实现WPF中按姓名分组的成绩统计
首先先把原始的成绩数据用表格列出来,方便参考:
| Name | Course | Score |
|---|---|---|
| John | Math | 5 |
| Hannah | Math | 4 |
| Lilly | Math | 5 |
| John | Science | 5 |
| Hannah | Science | 5 |
| Lilly | Science | 5 |
| John | Social | 5 |
| Hannah | Social | 4 |
| Lilly | Social | 3 |
我们的目标是按Name分组,计算每个学生的总分、平均分,以及星级(这里默认统计5分的出现次数,你可以按需修改规则)。
1. 定义数据模型类
首先需要两个简单的类:一个用来存储原始的单条成绩数据,另一个用来承载分组后的统计结果:
// 存储单条成绩记录的模型 public class StudentScore { public string Name { get; set; } public string Course { get; set; } public int Score { get; set; } } // 存储分组统计结果的模型 public class StudentStats { public string Name { get; set; } public int TotalScore { get; set; } public double AverageScore { get; set; } public int StarCount { get; set; } // 这里用来存指定分数的出现次数,比如5分 }
2. 用LINQ执行分组统计
接下来我们准备原始数据,然后通过LINQ的GroupBy方法分组,同时计算各项统计值:
// 模拟从数据源获取的原始成绩列表 List<StudentScore> rawScores = new List<StudentScore> { new StudentScore { Name = "John", Course = "Math", Score = 5 }, new StudentScore { Name = "Hannah", Course = "Math", Score = 4 }, new StudentScore { Name = "Lilly", Course = "Math", Score = 5 }, new StudentScore { Name = "John", Course = "Science", Score = 5 }, new StudentScore { Name = "Hannah", Course = "Science", Score = 5 }, new StudentScore { Name = "Lilly", Course = "Science", Score = 5 }, new StudentScore { Name = "John", Course = "Social", Score = 5 }, new StudentScore { Name = "Hannah", Course = "Social", Score = 4 }, new StudentScore { Name = "Lilly", Course = "Social", Score = 3 } }; // 核心的LINQ分组查询 var studentStatistics = rawScores .GroupBy(score => score.Name) // 按姓名分组 .Select(group => new StudentStats { Name = group.Key, TotalScore = group.Sum(s => s.Score), // 计算总分 AverageScore = Math.Round(group.Average(s => s.Score), 1), // 平均分保留1位小数 StarCount = group.Count(s => s.Score == 5) // 统计5分的次数作为星级 }) .ToList();
如果需要修改星级的统计规则(比如统计4分及以上的次数),只需要调整StarCount的计算逻辑:
StarCount = group.Count(s => s.Score >= 4)
3. 在WPF中绑定并展示结果
最后把统计结果绑定到WPF的DataGrid控件上,这里给出XAML和后台代码的示例:
XAML部分(窗口布局)
<Window x:Class="WpfScoreStats.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="学生成绩统计" Height="350" Width="550"> <Grid Margin="10"> <DataGrid ItemsSource="{Binding StatsList}" AutoGenerateColumns="False" CanUserAddRows="False" IsReadOnly="True"> <DataGrid.Columns> <DataGridTextColumn Header="姓名" Binding="{Binding Name}" Width="120"/> <DataGridTextColumn Header="总分" Binding="{Binding TotalScore}" Width="100"/> <DataGridTextColumn Header="平均分" Binding="{Binding AverageScore}" Width="100"/> <DataGridTextColumn Header="星级(5分次数)" Binding="{Binding StarCount}" Width="180"/> </DataGrid.Columns> </DataGrid> </Grid> </Window>
后台代码(设置数据上下文)
public MainWindow() { InitializeComponent(); // 把之前得到的统计结果绑定到DataContext DataContext = new { StatsList = studentStatistics }; }
最终统计结果
运行程序后,你会看到如下的统计表格:
| 姓名 | 总分 | 平均分 | 星级(5分次数) |
|---|---|---|---|
| John | 15 | 5.0 | 3 |
| Hannah | 13 | 4.3 | 1 |
| Lilly | 13 | 4.3 | 2 |
内容的提问来源于stack exchange,提问作者Dr.Bake
相关产品推荐
相关产品推荐

