提问人:AkshatSparrow 提问时间:10/4/2023 最后编辑:AkshatSparrow 更新时间:10/4/2023 访问量:64
.无行的 Sum() 返回类型
.Sum() return type for no rows
问:
我有两个表“问题”和“答案”。它们的关联如下:
public class Answer
{
public Guid Id { get; set; }
public Guid? QuestionId { get; set; }
public virtual Question? Question { get; set; }
}
public class Question
{
public Guid Id { get; set; }
[MaxLength(1024)] public string Question { get; set; }
public int? MaxMarks { get; set; }
}
评分系统仅适用于某些问题,这就是为什么可以为 null 的原因MaxMarks
TotalScore = s.Answers
.Where(a => a.Question.MaxMarks.HasValue)
.Sum(a => a.Question.MaxMarks)
我在这一行上遇到错误
System.InvalidCastException:无法将“System.Int64”类型的对象强制转换为“System.Int32”类型。 在 Microsoft.Data.SqlClient.SqlBuffer.get_Int32()
我试图显式地将其转换为 int ,但它不起作用。 有谁知道这里到底发生了什么?
此外,当没有向它传递任何行时,函数究竟会返回什么:( 或.Sum()
null
0
)
答: 暂无答案
评论
MaxMarks
long
int
long
MaxMarks
Question
bigint