var resultsFromSql = from wb in reportContext.WorkItems
where wi.DateRequired >= model.FirstDate && wi.DateRequired <= model.SecondDate
select new
{
wi.Reference,
dateRequired = SqlFunctions.DatePart("yyyy", wi.DateRequired) + "/" + SqlFunctions.DatePart("mm", wi.DateRequired) + "/" + SqlFunctions.DatePart("dd", wi.DateRequired),
earliestActionDate = SqlFunctions.DatePart("yyyy", wi.EarliestActionDate) + "/" + SqlFunctions.DatePart("mm", wi.EarliestActionDate) + "/" + SqlFunctions.DatePart("dd", wi.EarliestActionDate),
mainRef = wi.MainReferenceId != 0 ? wi.MainReferenceId.ToString() : " ",
subRef = wi.SecondaryReference ?? "",
details = wi.DisplayDetails ?? "",
createdOn = SqlFunctions.DatePart("yyyy", wi.CreatedOn) + "/" + SqlFunctions.DatePart("mm", wi.CreatedOn) + "/" + SqlFunctions.DatePart("dd", wi.CreatedOn),*/ /*wi.UpdatedBy,
updatedOn = SqlFunctions.DatePart("yyyy", wi.UpdatedOn) + "/" + SqlFunctions.DatePart("mm", wi.UpdatedOn) + "/" + SqlFunctions.DatePart("dd", wi.UpdatedOn),
timeTaken = SqlFunctions.DateDiff("day", wi.UpdatedOn, wi.CreatedOn) + " days "
/*+ SqlFunctions.DatePart("h", wi.UpdatedOn - wi.CreatedOn) + ":"
+ SqlFunctions.DatePart("n", wi.UpdatedOn - wi.CreatedOn) + ":"
+ SqlFunctions.DatePart("s", wi.UpdatedOn - wi.CreatedOn)*/,
timeStuff = SqlFunctions.DatePart("h", wi.UpdatedOn /*- wi.CreatedOn*/)
};
TimeTaken和TimeStuff由于DatePart不支持小时等而无法工作,我得到与timeTaken的注释部分的dateTime数学相关的其他错误。如何创建x天,小时,分钟和秒的timeTaken值,这是从updatedOn减去createdOn计算的时间长度,它们是dateTimes。
/*List<T> list = new List<T>();*/
foreach (var res in resultsFromSql.ToList())
{
res.timeTaken = res.CreatedOn; /*= SqlFunctions.DateDiff("day", res.UpdatedOn, res.CreatedOn)#1#
}
如果我之后运行foreach,我无法为任何属性赋值,因为匿名类型是readonly。如何以剩余时间(最多24小时),分钟数(最多60分)和秒数(最多60分钟)来获取我的日子(全天价值,而不是剩余部分,即197天)。
答案 0 :(得分:1)
仔细看看你在做什么,试试看:
var resultsFromSql = from wb in reportContext.WorkItems
where wi.DateRequired >= model.FirstDate && wi.DateRequired <= model.SecondDate
select new
{
wi.Reference,
dateRequired = wi.DateRequired.ToString("yyyy/mm/dd"),
earliestActionDate = wi.EarliestActionDate.ToString("yyyy/mm/dd"),
mainRef = wi.MainReferenceId != 0 ? wi.MainReferenceId.ToString() : " ",
subRef = wi.SecondaryReference ?? "",
details = wi.DisplayDetails ?? "",
createdOn = wi.CreatedOn.ToString("yyyy/mm/dd"),
updatedOn = wi.UpdatedOn.ToString("yyyy/mm/dd"),
timeTaken = new TimeSpan(wi.UpdatedOn.Ticks - wi.CreatedOn.Ticks).Days.ToString() + " days " +
new TimeSpan(wi.UpdatedOn.Ticks - wi.CreatedOn.Ticks).Hours.ToString() + ":" +
new TimeSpan(wi.UpdatedOn.Ticks - wi.CreatedOn.Ticks).Minutes.ToString() + ":" +
new TimeSpan(wi.UpdatedOn.Ticks - wi.CreatedOn.Ticks).Seconds.ToString()
};
只需在编辑器中输入,您可能需要稍微调整一下。这假定wi.DateRequired,wi.EarliestActionDate,wi.CreatedOn和wi.UpdatedOn是DateTime Type。还假设timeStuff是实验性的(尝试使其工作)并将其删除。
答案 1 :(得分:0)
public string ConvertToDayHourMinSec(DateTime firstTime, DateTime secondTime)
{
var timeTaken = new TimeSpan(firstTime.Ticks - secondTime.Ticks);
var days = timeTaken.ToString("%d");
var hms = timeTaken.ToString(@"hh\:mm\:ss");
return days + " days, " + hms;
}
感谢@ Kevin的回答让我走上了正确的道路。对于内联使用,代码有点重,所以我创建了上面的函数,可以使用任何dateTime调用,即
UpdatedMinusCreated = ConvertToDayHourMinSec(res.UpdatedOn, res.CreatedOn),
并提供格式4 days, 03:07:42