Ticks必须在0到TimeOnly.MaxValue.Ticks之间



我使用的是。net core 6,它支持DateOnly和TimeOnly数据类型。现在,我所遭受的是我从表(table1)中选择可空的TimeOnly列(TimeEnd),它给出例外:Ticks必须在0和TimeOnly. maxvalue .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();

您可以这样尝试:

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;}
}

然后将您的响应转换为允许空值的新创建对象:

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();
};

您应该添加代码EndTimeC = s.TimeEnd.HasValue ? s.TimeEnd.Value : default(TimeOnly)来检查TimeOnly是否为null,并根据它来处理

替代方式为EndTimeC = s.TimeEnd.GetValueOrDefault()

相关内容

  • 没有找到相关文章

最新更新