ASP.NET MVC Linq 查询 SQL 数据库 return 按名称求和
ASP.NET MVC Linq query to SQL DB return Sum by Name
我有 table 的数据库,其中包含学生分数。每条记录包含 StudentName (nvarchar) 和 Score (int from 1 to 5) 以及与记录相关的其他数据(日期、学科名称等)
------------------------------------------------
StudentName Score Discipline Date
Bob 5 Asp.net 05/23/21
Bob 5 Html 05/23/21
Bob 5 C# 05/23/21
John 4 Asp.net 05/23/21
John 4 C# 05/23/21
Michael 3 Asp.net 05/23/21
Michael 3 Html 05/23/21
Michael 4 C# 05/23/21
我正在尝试 return 结果视图,其中所有学生都可以看到他们的分数总和(按姓名)。
---------------------------
StudentName ScoreSum
Bob 15
John 8
Michael 10
我已尝试遵循 ,但 && 出现错误:
Operator 'operator' cannot be applied to operands of type 'type' and 'type'
Code is part of Controller and public ActionResult ViewStudentsBySumOfScore() is a method of it.
public ActionResult ViewStudentsBySumOfScore()
{
var db = new AllDbContext();
var ScoreSum = db.Scores
.Select(x =\> x.StudentName && x.Score)
.Sum();
return View();
}
如何更改代码以获得上述结果?
在为每个学生创建一个对象并使用.Sum()
为每个学生生成分数总和之前,您应该使用.GroupBy()
将所有分数记录按StudentName
分组。
如果您有一个 class 用于存储学生的总分,例如StudentScoreSum
:
public class StudentScoreSum
{
public string StudentName { get; set; }
public int ScoreSum { get; set; }
}
,实现方式如下:
List<StudentScoreSum> scoreSum = db.Scores
.GroupBy(s => s.StudentName)
.Select(scoresByStudent => new StudentScoreSum {
StudentName = scoresByStudent.Key,
ScoreSum = scoresByStudent.Sum(s => s.Score)})
.ToList();
示例 fiddle here.
.GroupBy()
return值
假设 Scores
数据库 table 中条目的数据类型实际上是 Score
,.GroupBy()
操作 return 是一个 IEnumerable<Grouping<string, Score>>
.
在.Select()
运算中,scoresByStudent
特此一个Grouping<string, Score>
;
string
是 组键的数据类型
Score
是组对象集合中元素的数据类型
根据您的示例分数数据,.GroupBy()
操作的结果将是三个分组。它们可以可视化为:
Key
Object collection
"Bob"
{ StudentName = "Bob", Score = 5 }
{ StudentName = "Bob", Score = 5 }
{ StudentName = "Bob", Score = 5 }
"John"
{ StudentName = "John", Score = 4 }
{ StudentName = "John", Score = 4 }
"Michael"
{ StudentName = "Michael", Score = 3 }
{ StudentName = "Michael", Score = 3 }
{ StudentName = "Michael", Score = 4 }
对于这三个组中的每一个,都会创建一个 StudentScoreSum
对象,使用组的 key 作为 StudentName
并将 Score
组的 对象集合 中所有元素的值,以生成 ScoreSum
.
我有 table 的数据库,其中包含学生分数。每条记录包含 StudentName (nvarchar) 和 Score (int from 1 to 5) 以及与记录相关的其他数据(日期、学科名称等)
------------------------------------------------
StudentName Score Discipline Date
Bob 5 Asp.net 05/23/21
Bob 5 Html 05/23/21
Bob 5 C# 05/23/21
John 4 Asp.net 05/23/21
John 4 C# 05/23/21
Michael 3 Asp.net 05/23/21
Michael 3 Html 05/23/21
Michael 4 C# 05/23/21
我正在尝试 return 结果视图,其中所有学生都可以看到他们的分数总和(按姓名)。
---------------------------
StudentName ScoreSum
Bob 15
John 8
Michael 10
我已尝试遵循
Operator 'operator' cannot be applied to operands of type 'type' and 'type' Code is part of Controller and public ActionResult ViewStudentsBySumOfScore() is a method of it.
public ActionResult ViewStudentsBySumOfScore()
{
var db = new AllDbContext();
var ScoreSum = db.Scores
.Select(x =\> x.StudentName && x.Score)
.Sum();
return View();
}
如何更改代码以获得上述结果?
在为每个学生创建一个对象并使用.Sum()
为每个学生生成分数总和之前,您应该使用.GroupBy()
将所有分数记录按StudentName
分组。
如果您有一个 class 用于存储学生的总分,例如StudentScoreSum
:
public class StudentScoreSum
{
public string StudentName { get; set; }
public int ScoreSum { get; set; }
}
,实现方式如下:
List<StudentScoreSum> scoreSum = db.Scores
.GroupBy(s => s.StudentName)
.Select(scoresByStudent => new StudentScoreSum {
StudentName = scoresByStudent.Key,
ScoreSum = scoresByStudent.Sum(s => s.Score)})
.ToList();
示例 fiddle here.
.GroupBy()
return值
假设 Scores
数据库 table 中条目的数据类型实际上是 Score
,.GroupBy()
操作 return 是一个 IEnumerable<Grouping<string, Score>>
.
在.Select()
运算中,scoresByStudent
特此一个Grouping<string, Score>
;
string
是 组键的数据类型Score
是组对象集合中元素的数据类型
根据您的示例分数数据,.GroupBy()
操作的结果将是三个分组。它们可以可视化为:
Key | Object collection |
---|---|
"Bob" | { StudentName = "Bob", Score = 5 } { StudentName = "Bob", Score = 5 } { StudentName = "Bob", Score = 5 } |
"John" | { StudentName = "John", Score = 4 } { StudentName = "John", Score = 4 } |
"Michael" | { StudentName = "Michael", Score = 3 } { StudentName = "Michael", Score = 3 } { StudentName = "Michael", Score = 4 } |
对于这三个组中的每一个,都会创建一个 StudentScoreSum
对象,使用组的 key 作为 StudentName
并将 Score
组的 对象集合 中所有元素的值,以生成 ScoreSum
.