Home > front end >  Ticks must be between 0 and and TimeOnly.MaxValue.Ticks
Ticks must be between 0 and and TimeOnly.MaxValue.Ticks

Time:12-29

I'm using .net core 6 which supports DateOnly and TimeOnly datatypes.Now, What I have sufferred from is I am selecting nullable TimeOnly column(TimeEnd) from table (table1) which gives exception as : Ticks must be between 0 and and TimeOnly.MaxValue.Ticks. (Parameter 'ticks')

var result = await (from s in _unitOfWork.Context.table1
               select new
            {
                Id = Convert.ToString(s.EventId),
                EndTimeC = s.TimeEnd,
                DateC = s.Date,
                StartTimeC = s.TimeBegin,
                Systemmodstamp = s.ModifiedDate}).ToListAsync();

CodePudding user response:

You can try it like this:

public class Result
{
  public string Id {get;set;}
  public TimeOnly? EndTimeC {get;set;}
  public DateOnly? DateC {get;set;}
  public TimeOnly? StartTimeC {get;set;}
  public DateOnly? Systemmodstamp {get;set;}
}

then convert your response to new created object that allowing null values:

var result = await (from s in _unitOfWork.Context.table1
               select new Result
            {
                Id = Convert.ToString(s.EventId),
                EndTimeC = s.TimeEnd,
                DateC = s.Date,
                StartTimeC = s.TimeBegin,
                Systemmodstamp = s.ModifiedDate}).ToListAsync();
             };

CodePudding user response:

you should add code EndTimeC = s.TimeEnd.HasValue ? s.TimeEnd.Value : default(TimeOnly) to check if TimeOnly is null or not and based on it you can handle it.

alternative way is EndTimeC = s.TimeEnd.GetValueOrDefault().

  • Related