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

  • Home
  • SEARCH
  • 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 144587
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T08:16:38+00:00 2026-05-11T08:16:38+00:00

I have this code (ok, I don’t, but something similar :p) var dogs =

  • 0

I have this code (ok, I don’t, but something similar :p)

    var dogs = Dogs.Select(ø => new Row     {             Name = ø.Name,             WeightOfNiceCats = ø.Owner               .Cats               .Where(æ => !æ.Annoying)               .Sum(æ => æ.Weight),     }); 

Here I go through all dogs and sum up the weight (into a non-nullable decimal) of all not-annoying cats which has the same owner as the dog. Of course, pretty much all cats are annoying, so I get this error:

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

None of the fields or foreign keys used can be null. So the error happens when the Where clause returns no cats, which it often does. But how can I solve this? I want it to return 0 when that happens. Tried with a DefaultIfEmpty() after the Where clause, but then I get this error:

Object reference not set to an instance of an object.

Which I guess is understandable. I tried to add a ?? after the Sum, but then it wont compile because of this error:

Operator ‘??’ cannot be applied to operands of type ‘decimal’ and ‘decimal’

Which also makes sense of course. So what can I do? Would be nice if the Sum thing just returned 0 when there was nothing to sum. Or a SumOrZero statement of some sort. Would it be difficult to make a SumOrZero method that worked with Linq2SQL?

  • 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. 2026-05-11T08:16:38+00:00Added an answer on May 11, 2026 at 8:16 am

    This is what I ended up with for now:

    .Sum(æ => (decimal?) æ.Weight) ?? 0.0M, 

    This works like a charm.

    I would still prefer to have a SumOrZero I could use, which would work exactly like the regular Sum except it would never return null for any reason. Like the others have noted, this would be pretty easy to make for IEnumerable but a bit more icky to create for IQueryable so it would work with Linq2SQL, etc. So I will just leave it at that for now. Although if someone is bored one day and do write a SumOrZero for IQueryable which works with Linq2SQL for all the numeric types, please do let me know 😀

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

Sidebar

Related Questions

I have this code var contacts = dr.mktDoctorContacts .GroupBy(x => x.ContactType) .Select(zb => new
I have this code to draw an ellipse in the screen but I don't
I have this code example, but I don't understand why changing the values in
This should be straight forward for a guru. I don't have any code really
I have this code while($row = mysql_fetch_row($result)) { echo '<tr>'; $pk = $row[0]['ARTICLE_NO']; foreach($row
I have this code, which works fine, but I would like to be able
I have this code that I don't really understand why it works: <html> <head>
At the moment I have this code (and I don't like it): private RenderedImage
I have this code with log4j, I don't use any kind of configuration files
I have this code below. I don't know why inArray() doesn't output 0. Any

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.