Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7949555
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T02:02:05+00:00 2026-06-04T02:02:05+00:00

I have a Method in my LINQ Basicly what my LINQ query does is

  • 0

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!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T02:02:06+00:00Added an answer on June 4, 2026 at 2:02 am

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a method that gets rows from my database. It looks like this:
I have a method GetOrder(int OrderID) that runs a LINQ query and returns an
I have the below LINQ method which gets called from a User Control and
I have following LINQ to SQL query expression from msg in TblUserMessages join user
I have a method who get a Iqueryable object get from a LINQ Query
I have a method that sets up my linq data context. Before it returns
I have a method that takes an IQueryable. Is there a LINQ query that
When manipulating data using Linq, how often does the SubmitChanges() method have to be
I have the below LINQ method that I use to create the empty EmploymentPLan
I have a method that contains the following Linq to SQL code: public List<L2SBusinessEntities.Report.MesReport>

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.