English 中文(简体)
在我的保存处采用准则处理方法,发现错误
原标题:Method using LINQ in my Repository that gets null errors

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

任何类型的帮助都受到高度赞赏!

提前感谢!

最佳回答

看看询问的最后一个部分——如果在过滤<条码>后没有退行,则条款,那么<条码>,那么GoalCard QuestionAnswer.Grade.Value将无限期地扔下,因为你试图获取一个无效物体的特性。

我相信,如果你把该守则改成最后价值,那么你就必须在你的法典中明确检查这一价值。

.Average(s => s != null ? s.GoalCardQuestionAnswer.Grade.Value : 0)

否则,打破僵局将是一个好主意,有助于法典的瓦解和可读性。

问题回答

I would split up the queries and investigate further. My guess would be that there is some inconsistent data. Probably it happens somewhere where you compare a double with a double? and the double? value is null. Maybe check your records for nulls, which should actually be there.

当你试图向没有记录的结果申请总操作员时,通常会出现这一错误。 看看你使用的地方。 平均和我确信,你有一个疑问,即以前没有记录。





相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签