-3

I am trying to sum my timespan. My logic is working absolutely fine, he only problem is if value is NULL then my application breaks. I want to pass "00:00:00" as my time-span if value is NULL. My following code is not working:

@Model.Sum(x=> TimeSpan.Parse(x.Time ?? "00:00:00").Minutes)

X.Time is string and in my logic it could be NULL.If it is NULL, I want to replace it with 00:00:00

What am I doing wrong?

6 Answers6

2

I guess you want to sum the total of minutes, so you can simply check for null and add 0 to the total amount:

@Model.Sum(x=> !string.IsNullOrEmpty(x.Time) ? TimeSpan.Parse(x.Time).TotalMinutes : 0)
Patrick Hofman
  • 153,850
  • 22
  • 249
  • 325
1

Instead of checking all values null or not, you can filter before in order to have only not null values.

@Model.Where(x => !string.IsNullOrEmpty(x.Time)).Sum(x=> TimeSpan.Parse(x.Time).Minutes)
Coskun Ozogul
  • 2,389
  • 1
  • 20
  • 32
1

The TimeSpan.Parse method expects a parameter in the form

[ws][-]{ d | [d.]hh:mm[:ss[.ff]] }[ws]

since 00:00:00 is a valid input string, I'm taking a wild guess here and say that it's not about null values, but rather the error occurs when your x.Time was set by DateTime.Now.ToString() etc. because in those cases you get the

System.FormatException: 'String was not recognized as a valid TimeSpan.'

Your code works perfectly fine for Time being null. Please make sure the other values are valid.

Thomas Flinkow
  • 4,845
  • 5
  • 29
  • 65
1

The problem with your question is that from what you say, the code should work. For example, the following code works correctly:

using System;
using System.Collections.Generic;
using System.Linq;

namespace Demo
{
    class Test
    {
        public string Time;
    }

    class Program
    {
        static void Main()
        {
            var Model = new List<Test>
            {
                new Test{Time = "01:01:01"},
                // null, // Uncomment this line to make it crash.
                new Test{Time = null}, 
                new Test{Time = "02:02:02"}
            };

            int result = Model.Sum(x => TimeSpan.Parse(x.Time ?? "00:00:00").Minutes);

            Console.WriteLine(result);
        }
    }
}

Since you say an error is occurring, there must be something else going wrong. I think that the issue is that x itself is null.

We can demonstrate that possibility by uncommenting the indicated line. If you do that, the program will crash.

If that is indeed the problem, then the solution is very simple; just change the summation to:

int result = Model.Sum(x => TimeSpan.Parse(x?.Time ?? "00:00:00").Minutes);

After making that change, a null x will not make it crash.

Note that it may be slightly better to write:

int result = Model.Sum(x => x?.Time != null ? TimeSpan.Parse(x.Time).Minutes : 0 );

because then you avoid parsing "00:00:00" only to return zero (via .Minutes), but the difference is likely to be very marginal if any.

Matthew Watson
  • 104,400
  • 10
  • 158
  • 276
  • 1
    +1 nice answer, especially the part about the unknown exception. It's really hard to tell what the error is when it's not stated in the question; e.g. you and Pervez assume a `NullReferenceException`, I assume a `FormatException` but in the end, unless OP edit's their answer, we can never know. – Thomas Flinkow Apr 12 '18 at 08:16
  • 1
    @ThomasFlinkow You're right - it could be a `FormatException`. The question is really a bit too iffy to answer, but hopefully one of these answers will elicit more information from the OP! – Matthew Watson Apr 12 '18 at 08:17
0

Your are getting a null exception because x is null. So when you call the property x.Time, it will throw a NullReferenceException. You can use the ternary operator to check if x is null

 @Model.Sum(x => TimeSpan.Parse(x == null ? "00:00:00" : x.Time).Minutes)
Pervez Alam
  • 2,141
  • 1
  • 8
  • 6
0

Try this one

TimeSpan? variable = (TimeSpan?)null.

This value return null value.

Raviteja V
  • 454
  • 5
  • 11