473,406 Members | 2,894 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

Nullable object must have a value.

Hello,

I am trying to define a value for a statistic as follows:

FilesPerMonth = db.Files.Count() /
(float)((DTH.Span(DI.Month, db.Files.Min(f =>
f.CreatedAt), DateTime.Now) ?? 1) == 0 ? 1 :
DTH.Span(DI.Month, db.Files.Min(f =>
f.CreatedAt), DateTime.Now).Value),

FilesPerMonth is a float

Basically, I check if DTH.Span returns null ... if it does I define a
default value = 1.
If not I check if the value is 0. If yes I give it the value 1 to
prevent division by 0.
Otherwise I use the returned value.

my DTH.Span function is:

public static long? Span(DateInterval interval, DateTime? from,
DateTime? to)

I am using this inline code because I am using this on a Linq Query.

Does anyone knows what I am doing wrong. I chenged my code many times
but I keep having the same problem:

"Nullable object must have a value."

I though I had preview all possible situations on my code but I am
missing something.

Thank You,
Miguel

Oct 20 '08 #1
2 5261
In the first test, you use null-coalescing (?? 1) - but you don't do
this in the second. So if the thing *is* null, the first test fails
(since null ?? 1 is not zero, so the second version is evaluated which
yields null. I suspect you want to use null-coalescing in the second
version, too.

I don't know if your link provider supports it, but you might be able
to use .GetValueOrDefault(1) to do this (in both cases).

Marc
Oct 20 '08 #2
On Oct 20, 5:23*am, Marc Gravell <marc.grav...@gmail.comwrote:
In the first test, you use null-coalescing (?? 1) - but you don't do
this in the second. So if the thing *is* null, the first test fails
(since null ?? 1 is not zero, so the second version is evaluated which
yields null. I suspect you want to use null-coalescing in the second
version, too.

I don't know if your link provider supports it, but you might be able
to use .GetValueOrDefault(1) to do this (in both cases).

Marc
Thanks! Didn't know about GetValueOrDefault. Was reading about it.
Really useful.
I solved my problem without it but I started using it because it
simplifies it a lot.

Thank You!
Miguel
Oct 20 '08 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

14
by: ago | last post by:
Is it possible to have a default value associated python objects? I.e. to flag an attribute in such a way that the assignment operator for the object returns the default attribute instead of the...
2
by: Aaron | last post by:
I have a data sructure setup and I populate it in a loop like so: y=0 while X: DS.name = "ASDF" DS.ID = 1234 list = DS; y = y + 1
3
by: Nemisis | last post by:
Hi everyone, i have been stressing over this for the past hour or so, and just cannot figure it out, sorry if i am being stupid, but it is a friday! lol I have the following Private _ID as...
15
by: scparker | last post by:
I have yet to find a satisfactory solution to this problem. It involves VB.NET 2.0 and datetime issues. I have a form that asks for a Date to be submitted in dd/mm/yyyy format. When this is...
5
by: =?Utf-8?B?emlubw==?= | last post by:
in .net 2.0, the class (myClass) contains a property defined as: private _creationDate as As Nullable(Of DateTime) Public Property CreationDate() As Nullable(Of DateTime) Get If...
6
by: active | last post by:
I have an image and a graphics object created (FromImage) from that image. I need to create a new image and create a new graphics object from the new image. I want the new graphics object have...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.