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 am new with Linq and I would like to sort some data that
still new to the world of linq, and i need some help flatening a
How do I do this in linq? var p = new pmaker(); foreach (var
I'm familiar with .NET and with SQL. Now I'm looking at the new LINQ
I am completely new to LINQ in C#/.NET. I understand that I could use
I'm new to using LINQ to Entities (or Entity Framework whatever they're calling it)
As far as I can understand, when I new up a Linq to SQL
I'm new to both C# and Linq. I'm attempting to use a Linq-generated element
I've got a new instance of an object in a Linq DBML, but don't
I'm confused about Linq to Entities. Is it the new name of Entity Framework

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.