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 4036520
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T12:13:59+00:00 2026-05-20T12:13:59+00:00

I am building a little application in .NET 4 MVC 2 for the office

  • 0

I am building a little application in .NET 4 MVC 2 for the office foosball table. I have this repository with a method summing all the values of one column (decimal) using LINQ to SQL. It looks like this:

public decimal GetPlayerRating(int id)
{
    var playerRating = (from r in db.Ratings
                        where r.PlayerID == id
                        select r.Points).Sum();

    return playerRating;
}

The problem is that sometimes when it’s called it returns null because the given player does not have any records in the table. That’s fine — in that case it should just return zero. But I get this error:

The null value cannot be assigned to a
member with type System.Decimal which
is a non-nullable value type.

Description: An unhandled exception
occurred during the execution of the
current web request. Please review the
stack trace for more information about
the error and where it originated in
the code.

Exception Details:
System.InvalidOperationException: The
null value cannot be assigned to a
member with type System.Decimal which
is a non-nullable value type.

How can I tell my method that it is okay to return a null value? Thanks!

  • 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-05-20T12:14:00+00:00Added an answer on May 20, 2026 at 12:14 pm

    Try the null coalese operator

    return playerRating ?? 0d;
    

    That will never return a null.

    If you really need to return a nullable valuetype, though try this

    public decimal? GetPlayerRating(int id)
    { ... }
    

    Adding a ? to the end of a value type like double is rewritten into Nullable < type > (Nullable < double > here)

    When dealing with a nullable value type, be sure to check if it has a value (not null)

    if ( myNullableType.HasValue ) 
    {
        x = myNullableType.Value;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm building an asp.net MVC application where users can attach a picture to their
I have a strange issue with an Asp.NET MVC application. Using Asp.NET MVC 3
I have a little (and stupid) problem: I'm building a PHP application using mysqli
We're building a business application using Microsoft ASP.NET MVC 3. Some views are now
I am building a little web based application using an HTML table. One unusual
I'm building a little search application (I know I can use match against). But
After building the application GUI, I realised it was a little bit too complex
i'm building this little function, what i need is this query to print me
I'm building my first real rails application for a little in-house task. Most of
I'm building a Qt application that have about 30 different views (QWidgets). My idea

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.