Obsivus
Obsivus

Reputation: 8359

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.

A user can choose between two dates and retrieve the data between those two dates. But if a user choose on a date range which there is no data in I recieve this error:

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.

Here is the method:

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();
}

Update: Grade is Double and Nullable

Any kind of help is very appreciated!

Thanks in advance!

Upvotes: 2

Views: 797

Answers (3)

Joanna Derks
Joanna Derks

Reputation: 4063

Looking at the last part of the query - if there are no rows returned after filtering with the where clause, then s.GoalCardQuestionAnswer.Grade.Value will definitely throw because you're trying to access properties of an object that is null.

I suppose that if you change the code to this you would get 0 as the final value - then you'd need to check for this value explicitly in your code.

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

Otherwise, breaking the query down would be a good idea - it helps both debugging and readability of the code.

Upvotes: 1

TheGeekYouNeed
TheGeekYouNeed

Reputation: 7539

That error usually comes up when you are trying to apply an aggregate operator to a result set with no records. Look at where you are using .Average and I am sure you have a query that returns no records right before.

Upvotes: 0

Michael Schnerring
Michael Schnerring

Reputation: 3661

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.

Upvotes: 0

Related Questions