C#中字典中的groupby
groupby in dictionary in C#
var dict = new Dictionary<ClassA,int>();
我应该将姓名和座位号添加到字典中,但我必须从单独的 类 中获取它,
例如
dict.Add(student.name)
dict.Add(class.studentlist.score)
我希望它得到
的输出
Student Score
Amy 78
Amy 89
Amy 45
Ben 34
.
.
.
以及如果学生的名字重复多次则显示总分的分数
Student Score
Amy (total score)
Ben (total score)
我不确定该怎么做,或者是否可能?名字和分数来自不同的 类 所以我有点困惑。
字典中不能有多个名称键,名称应该是唯一的。所以尝试创建一个列表
var students = new List<Student>();
students.Add(new Student { Name = "Amy", Score = 78 });
students.Add(new Student { Name = "Ben", Score = 34 });
students.Add(new Student { Name = "Amy", Score = 89 });
students.Add(new Student { Name = "Amy", Score = 45 });
List<string,int> scores = students.GroupBy(s => s.Name)
.Select(i => new Student { Name = i.Key, Score = i.Sum(x => x.Score) }).ToList();
public class Student
{
public string Name { get; set; }
public int Score { get; set; }
}
更新
@Cleptus 建议使用 Dictionary ,其中 string 是名称,list 是保存分数。这是一个非常有趣的想法,但我更喜欢列表,因为它更像是一个关系数据库,而 linq 是列表集合的理想选择。我觉得字典太分层了,总是需要额外的步骤或代码来获取一些信息。
但是可以用字典来保存结果信息
Dictionary<string, int> result = students.GroupBy(s => s.Name)
.ToDictionary( i => i.Key, i=> i.Sum(x => x.Score));
var amyResult=result["Amy"];
假设您的 class 学生是:
public class Student
{
public string Name { get; set; }
}
您可以使用 Dictionary
,其键为 Student
,其内容为 list/array 分数。
List<Student, List<int>> results = new List<Student, List<int>>();
results.Add(new Student() { Name = "Amy"}, new List<int>() { 78, 89, 45 });
results.Add(new Student() { Name = "Ben"}, new List<int>() { 61 });
要显示数据,您只需迭代键并根据需要显示数据(聚合或单独显示)。
using System.Linq;
....
foreach (Student currentStudent in results.Keys) {
List<int> studentResults = results[currentStudent];
// This would show your first needed output (individual scores)
foreach(int result in studentResults) Console.WriteLine(currentStudent.Name + ": " + result.ToString());
// This would show your second needed output (total scores)
Console.WriteLine(currentStudent.Name + ": " + studentResults.Sum().ToString());
}
第二个利用IEnumerable.Sum()
var dict = new Dictionary<ClassA,int>();
我应该将姓名和座位号添加到字典中,但我必须从单独的 类 中获取它, 例如
dict.Add(student.name)
dict.Add(class.studentlist.score)
我希望它得到
的输出Student Score
Amy 78
Amy 89
Amy 45
Ben 34
.
.
.
以及如果学生的名字重复多次则显示总分的分数
Student Score
Amy (total score)
Ben (total score)
我不确定该怎么做,或者是否可能?名字和分数来自不同的 类 所以我有点困惑。
字典中不能有多个名称键,名称应该是唯一的。所以尝试创建一个列表
var students = new List<Student>();
students.Add(new Student { Name = "Amy", Score = 78 });
students.Add(new Student { Name = "Ben", Score = 34 });
students.Add(new Student { Name = "Amy", Score = 89 });
students.Add(new Student { Name = "Amy", Score = 45 });
List<string,int> scores = students.GroupBy(s => s.Name)
.Select(i => new Student { Name = i.Key, Score = i.Sum(x => x.Score) }).ToList();
public class Student
{
public string Name { get; set; }
public int Score { get; set; }
}
更新
@Cleptus 建议使用 Dictionary
但是可以用字典来保存结果信息
Dictionary<string, int> result = students.GroupBy(s => s.Name)
.ToDictionary( i => i.Key, i=> i.Sum(x => x.Score));
var amyResult=result["Amy"];
假设您的 class 学生是:
public class Student
{
public string Name { get; set; }
}
您可以使用 Dictionary
,其键为 Student
,其内容为 list/array 分数。
List<Student, List<int>> results = new List<Student, List<int>>();
results.Add(new Student() { Name = "Amy"}, new List<int>() { 78, 89, 45 });
results.Add(new Student() { Name = "Ben"}, new List<int>() { 61 });
要显示数据,您只需迭代键并根据需要显示数据(聚合或单独显示)。
using System.Linq;
....
foreach (Student currentStudent in results.Keys) {
List<int> studentResults = results[currentStudent];
// This would show your first needed output (individual scores)
foreach(int result in studentResults) Console.WriteLine(currentStudent.Name + ": " + result.ToString());
// This would show your second needed output (total scores)
Console.WriteLine(currentStudent.Name + ": " + studentResults.Sum().ToString());
}
第二个利用IEnumerable.Sum()