Search code examples
c#asp.net-mvcmodel-view-controllerasp.net-mvc-viewmodel

Cannot implicitly convert type 'bool' to 'System.DateTimeOffset?'


Just trying to get a list that does have data in one of my view models. I have got so that if there is NO data it lists but I did it so that it does have data.

It's just throwing the error in the title. Here is the code listing:

    public List<RegisterMark> HasRegistered
        => RegisterMarks
            .Where(rm => rm.TimeRegistered is true)
            .ToList();

Many thanks in advance!


Solution

  • You should reformulate the where expression to test for non-null:

    .Where(rm => rm.TimeRegistered != null)
    

    The reason: When a property is not set, it either has a default value or is null. If its the latter, you can simply test for it.

    Do note is is only used in c# to do a type comparison.