Asp.net 在c中从给定日期时间中查找最大日期时间#
我正在数据库(SQL Server)中存储关联生产和非生产详细信息。我的任务是,如果它们是数据库中当前日期的任何记录(在生产表或非生产表或两者中),则从这两个表中获取任务提交时间。到目前为止,我已经完成了Asp.net 在c中从给定日期时间中查找最大日期时间#,asp.net,c#-4.0,Asp.net,C# 4.0,我正在数据库(SQL Server)中存储关联生产和非生产详细信息。我的任务是,如果它们是数据库中当前日期的任何记录(在生产表或非生产表或两者中),则从这两个表中获取任务提交时间。到目前为止,我已经完成了 if ((_timeEntryId > 0) || (_timeEntryIDNonProduction > 0)) { if (_timeEntryId > 0) { DateTime lastProductiontaskTIme = _pr
if ((_timeEntryId > 0) || (_timeEntryIDNonProduction > 0))
{
if (_timeEntryId > 0)
{
DateTime lastProductiontaskTIme = _production.GetLastTaskTime(Convert.ToInt32(_timeEntryId));
}
if (_timeEntryIDNonProduction > 0)
{
DateTime lastNonProductionTime= _nonProduction.GetLastTaskTime(Convert.ToInt32(_timeEntryId));
}
}
现在,我的要求是从lastProductiontaskTIme和lastNonProductionTime中确定最长的日期时间(最新的)。我是如何从这两者中找到最新的日期时间的。请帮我解决这个问题
DateTime lastTime;
if ((_timeEntryId > 0) || (_timeEntryIDNonProduction > 0))
{
DateTime lastProductiontaskTime;
if (_timeEntryId > 0)
{
lastProductiontaskTime = _production.GetLastTaskTime(Convert.ToInt32(_timeEntryId));
}
DateTime lastNonProductionTime;
if (_timeEntryIDNonProduction > 0)
{
lastNonProductionTime = _nonProduction.GetLastTaskTime(Convert.ToInt32(_timeEntryId));
}
lastTime = lastProductiontaskTime > lastNonProductionTime ? lastProductiontaskTime : lastNonProductionTime;
}
这将获得两个值中的最高值
DateTime
结构可以像整数一样进行比较,最高值出现在时间的后面。您也可以使用数学:
lastTime = new DateTime(Math.Max(lastProductiontaskTime.Ticks, lastNonProductionTime.Ticks))