1

In my Asp.net core project, I need to check for null TimeSpan properties. This is my method:

 return result.Include(e => e.Episodes).AsEnumerable().Select(c => new ShowListCoursesViewModel()
            {
                CourseId = c.CourseId,
                CourseImageName = c.CourseImageName,
                CoursePrice = Convert.ToDecimal(c.CoursePrice),
                CourseTitle = c.CopurseTitle,
                CourseTotalTime = new TimeSpan(c.Episodes.Sum(t => t.EpisodeTimeLength.Value.Ticks) )
            }).Skip(skip).Take(take).OrderByDescending(d => d.CoursePrice).ToList();

It's showing a green squiggle under the t.EpisodeTimeLength and saying:

Nullable value type may be null.

My CourseTotalTime property is nullable and also the EpisodeTimeLength is nullable too.

How can I interpret null values of EpisodeTimeLength as 00:00:00 in the Sum? Example: [null, 01:00:00, 01:00:00] should yield 02:00:00 in CourseTotaltime.

ProgrammingLlama
  • 36,677
  • 7
  • 67
  • 86
Hossein
  • 142
  • 10
  • So you want to assign `null` to `CourseTotalTime` if _any_ `EpisodeTimeLength` is `null`, even if other values of `EpisodeTimeLength` belonging to the same course have non-null values? Is that correct? – ProgrammingLlama Oct 26 '21 at 01:47
  • No, I just want to have sum of my `EpisodeTimeLength` then if one of my `EpisodeTimeLength`s was null, considered it as "00:00:00". – Hossein Oct 26 '21 at 01:55
  • So you want `CourseTotalTime` to be 00:00:00 if any `EpisodeTimeLength` is `null`? – ProgrammingLlama Oct 26 '21 at 01:56
  • 1
    Then you don't want to assign 00:00:00 to `CourseTotalTime` as you stated in your question. I suggest you edit your question to fix this. – ProgrammingLlama Oct 26 '21 at 02:03
  • I want the `Sum()` to consider the null as `00:00:00`. that's it – Hossein Oct 26 '21 at 02:04
  • I don't think so. as i said in my question, I want to get rid of the green squiggle. So Idecided to replace the null values with `00:00:00` to be able to get rid of green squiggle! – Hossein Oct 26 '21 at 02:10
  • @Llama would you take a look at this one please ? https://stackoverflow.com/questions/69749529/how-to-order-the-entities-by-their-timespan-properties/69749661?noredirect=1#comment123297117_69749661 – Hossein Oct 28 '21 at 12:57

1 Answers1

3

You can exclude null values with Where before the Sum:

CourseTotalTime = new TimeSpan(
    c.Episodes
        .Where(t => t.EpisodeTimeLength.HasValue)
        .Sum(t => t.EpisodeTimeLength.Value.Ticks))

Alternatively, you can specify a default value to use when t.EpisodeTimeLength is null:

CourseTotalTime = new TimeSpan(
    c.Episodes
        .Sum(t => (t.EpisodeTimeLength ?? TimeSpan.Zero).Ticks))

This makes use of the null-coalescing operator.

Demonstration of both approaches

ProgrammingLlama
  • 36,677
  • 7
  • 67
  • 86
  • Yes. that's the thing that I was looking for. But in this case, the ';' symboles at the end of the sntences should be omitted :). Thank You very much. – Hossein Oct 26 '21 at 02:25
  • Sorry that was a carry-over from my own testing :) I've removed the semicolons now. – ProgrammingLlama Oct 26 '21 at 02:26