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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T08:32:20+00:00 2026-05-12T08:32:20+00:00

I am new to LINQ and am trying to create some data points from

  • 0

I am new to LINQ and am trying to create some data points from a table to graph. The three fields of importance in this table are the id, the time and the value. I am writing a query to get the average value over a set time for a chosen id. The LINQ I have written follows:

var value = (from t in _table
             where t.Id == id
                 && t.Time >= intervalStartTime
                 && t.Time <= intervalEndTime
             select t.Value).Average();

However this crashes at runtime with:

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

At certain intervals there is no data so the SQL LINQ generates returns null, which I would liked to be COALESCED to 0 but instead crashes the application. Is there a way to write this LINQ query to be able to handle this properly?

The table definition to make things clearer:

[Serializable]
[Table(Name = "ExampleTable")]
public class ExampleTable
{
    [Column(Name = "Id")]
    public int Id { get; set; }

    [Column(Name = "Time")]
    public DateTime Time { get; set; }

    [Column(Name = "Value")]
    public int Value{ get; set; }
}
  • 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-12T08:32:20+00:00Added an answer on May 12, 2026 at 8:32 am

    I think you want

    var value = (from t in _table
                 where t.Id == id
                    && t.Time >= intervalStartTime
                    && t.Time <= intervalEndTime
                 select (int?)t.Value).Average()
    

    This way, you get a double? back, whereas without the (int?) cast you need to get a double back, which cannot be null.

    This is because of the signatures

    double Enumerable.Average(IEnumerable<int> source)
    double? Enumerable.Average(IEnumerable<int?> source)
    

    Now, to get an average of 0 instead of null, you need to place the coalescing operator at the end

    var value = (from t in _table
                 where t.Id == id
                    && t.Time >= intervalStartTime
                    && t.Time <= intervalEndTime
                 select (int?)t.Value).Average() ?? 0.0;
    

    IMHO this is a pretty awful design of the Enumerable/Queryable class; why can’t Average(IEnumerable<int>) return double?, why only for Average(IEnumerable<int?>)?

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

Sidebar

Related Questions

I'm trying to use LINQ to create a new dictionary from an existing one,
I am new with Linq and I would like to sort some data that
Trying to create a list to return some JSON data to a view. Following
I am trying to serialize some Linq objects using this code. private byte[] GetSerializedObj(object
I am using Entity Framework with Linq to Entities, trying to select some data
Still a bit new to Linq. This is driving me nuts. I want to
I'm trying to get an insert to work using LINQ and am having some
I'm new to LINQ, and I have trouble with a query I'm trying to
I am trying to create an automated clicker program in C# to make some
I spent this morning in trying to figure out where the system.linq.expressions namespace is.

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.