I have a Method in my LINQ Basicly what my LINQ query does is that it retrieves data from my database.
用户可在两个日期之间选择并检索数据。 但是,如果用户选择某一日期范围,我就没有数据,那么这个错误就会发生:
The cast to value type Double failed because the materialized value is null. Either the result type s generic parameter or the query must use a nullable type
It should be okey for the users to search between the two dates even if there is no data, it should give a error message that there is no data between those dates instead of this error I recieve in my Visual studio. I am using MVC entity framework model first.
这里的方法是:
public List<CoreValueAndAverageGrade> GetAverageGradeForAllCoreValues(
OfficeStatisticQueryViewModel model)
{
var StartDate = DateTime.Parse(model.StartDate);
var EndDate = DateTime.Parse(model.EndDate);
return db.CoreValue
.Where(v => v.CoreValueQuestion
.Any(q => !q.SubjectType.Ignored_Statistic))
.Select(coreValue => new CoreValueAndAverageGrade
{
CoreValue = coreValue,
AverageGrade = coreValue.CoreValueQuestion
.Where(q => !q.SubjectType.Ignored_Statistic)
.Average(q => q.SelectedQuestions
.Where(s =>
s.GoalCardQuestionAnswer != null
&& s.GoalCardQuestionAnswer.Grade.HasValue
&& s.GoalCard.Completed_Date >= StartDate
&& s.GoalCard.Completed_Date <= EndDate
)
.Average(s => s.GoalCardQuestionAnswer.Grade.Value))
})
.ToList();
}
最新信息:Grade is Double
and Nullable
任何类型的帮助都受到高度赞赏!
提前感谢!