473,385 Members | 1,907 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,385 software developers and data experts.

TimeSpan

Hello,

I have the following:

boxStat = new BoxStat {
BoxCount = database.Boxes.Count(),
SinceLastCreate = (DateTime.Now - database.Boxes.Max(b =>
b.CreatedAt)).Days ?? 0
};

I am getting an error:
'System.Nullable<System.TimeSpan>' does not contain a definition for
'Days' and no extension method 'Days' accepting a first argument of
type 'System.Nullable<System.TimeSpan>' could be found (are you
missing a using directive or an assembly reference?)

I added ?? 0 to try to solve the problem but I keep having the same
error.

What am I doing wrong?

Thank You,
Miguel
Oct 17 '08 #1
2 12555
For whatever reason , DateTime math has always seemed to require an explicit
cast to TimeSpan :

SinceLastCreate = ( (TimeSpan) (DateTime.Now - database.Boxes.Max(b
=b.CreatedAt)) ).Days
"shapper" <md*****@gmail.comwrote in message
news:85**********************************@p49g2000 hsd.googlegroups.com...
Hello,

I have the following:

boxStat = new BoxStat {
BoxCount = database.Boxes.Count(),
SinceLastCreate = ((TimeSpan)(DateTime.Now - database.Boxes.Max(b
=>
b.CreatedAt))).Days ?? 0
};

I am getting an error:
'System.Nullable<System.TimeSpan>' does not contain a definition for
'Days' and no extension method 'Days' accepting a first argument of
type 'System.Nullable<System.TimeSpan>' could be found (are you
missing a using directive or an assembly reference?)

I added ?? 0 to try to solve the problem but I keep having the same
error.

What am I doing wrong?

Thank You,
Miguel

Oct 17 '08 #2
shapper has brought this to us :
Hello,

I have the following:

boxStat = new BoxStat {
BoxCount = database.Boxes.Count(),
SinceLastCreate = (DateTime.Now - database.Boxes.Max(b =>
b.CreatedAt)).Days ?? 0
};

I am getting an error:
'System.Nullable<System.TimeSpan>' does not contain a definition for
'Days' and no extension method 'Days' accepting a first argument of
type 'System.Nullable<System.TimeSpan>' could be found (are you
missing a using directive or an assembly reference?)

I added ?? 0 to try to solve the problem but I keep having the same
error.

What am I doing wrong?

Thank You,
Miguel
What does "database.Boxes.Max()" return? I'm guessing a nullable
DateTime (either "DateTime?" or "Nullable<DateTime>", they are the
same).

That means that that difference will not return a TimeSpan, but a
nullable TimeSpan.
That "TimeSpan?" does not have a "Days" property, but it has a Value
property that returns a TimeSpan. That *does* have a Days property.
(DateTime.Now - database.Boxes.Max(b =b.CreatedAt)).Value.Days

Note: this fails if that Max returns a null!
You could have used that ?? operator, but then at the point where a
nullable type was involved:
((DateTime.Now - database.Boxes.Max(b =b.CreatedAt)) ??
TimeSpan.Zero).Days
This way you get 0 days when Max returned null.

Or (as gerry suggested) cast to TimeSpan before reading the Days
property. But this also fails when Max returns null.

Hans Kesting
Oct 17 '08 #3

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

Similar topics

2
by: Jeff Shantz | last post by:
Hello, I'm developing a large statistics application for a call center. It often needs to calculate time spanning over months. For example, an agent's total talk time within 30 days. Since an...
3
by: Ivan A. | last post by:
Hi! Why I can't serialize TimeSpan structure with XmlSerializer? This is what I do: using System; using System.IO; using System.Xml; using System.Xml.Serialization;
11
by: Russ Green | last post by:
How does this: public TimeSpan Timeout { get { return timeout; } set { timeout = value; if(timeout < licenseTimeout) licenseTimeout = timeout; }
2
by: DWalker | last post by:
In Visual Studio (Visual Basic) .NET 2002, I noticed that this: Dim Elapsed as DateTime = Now - Now gives a "compile time" error (error in the IDE), saying that the '-' operator is not...
10
by: Charles Law | last post by:
If I display a TimeSpan I get something like 00:05:17.6217891 when what I would like to see is 00:05:18 Is there an easy way to get this output? Try as I might I just can't find it.
2
by: ucasesoftware | last post by:
i translate a C# funtion to VB.NET and i have this : Shared Function isAllDay(ByVal ap As Appointment) As Boolean Return ap.DateBegin.TimeOfDay = TimeSpan.Zero AndAlso ap.DateEnd.TimeOfDay =...
2
by: Dennis D. | last post by:
Hello: I want to subtract a timespan from the selectionrange.start of a month calendar. The selectionrange.start of a month calendar is a date, and it is possible to subtract a timespan from a...
13
by: sd00 | last post by:
Hi all, can someone give me some coding help with a problem that *should* be really simple, yet I'm struggling with. I need the difference between 2 times (Target / Actual) However, these times...
5
by: style | last post by:
Hello In my custom configuration section, I'd like to specify an interval attribute which specifies an interval in milliseconds. I think it would be very comfortable to get this value directly...
4
by: Massimo | last post by:
Hi to All, i'm using C# in .NET 2.0 and i have a DataTable A with a column of type TimeSpan used to store HOUR info. I'm trying to filter my DataTable A selecting only rows that have the column...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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?
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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

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.