Nullable DateTimes and the AddDays() extension

前端 未结 4 1173
暗喜
暗喜 2021-01-12 02:34

I have a DateTime variable that can either be null or a Datetime. I figured a nullable DateTime type would work, but I am getting an error telling me that said

4条回答
  •  无人及你
    2021-01-12 03:15

    You should consider trying to make your logic more readable:

    var inviteNudgeFlag = bool.Parse((string) Session["InviteNudgeFlag"]);
    
    if(!inviteNudgeFlag)
    {
        return;
    }
    
    var lastInvite = (DateTime?) Session["LastInviteSent"];   
    var inviteCount = (int) Session["InviteCount"];
    var numOfDays = 7;
    var now = DateTime.Now;
    
    var weekSinceLastInvite = lastInvite.HasValue
                                ? now >= lastInvite.Value.AddDays(numOfDays)
                                : now >= AcctCreation.AddDays(numOfDays);
    
    var hasInvites = !lastInvite.HasValue || inviteCount > 0;
    var canInvite = hasInvites && weekSinceLastInvite;
    
    if(!canInvite)
    {
        return;
    }
    

提交回复
热议问题