條件這是我的代碼:我設置的參數如何基於使用LINQ和Entity Framework
await ctx.Education.AsNoTracking()
.Where(e => e.EmployeeNumber == employeeNumber)
.Select(e => new EducationDTO {
Id = e.EducationID,
StartDate = DbFunctions.CreateDateTime(e.EducationEntryYear , 1, 1, 0, 0, 0) ?? DateTime.Now,
EndDate = DbFunctions.CreateDateTime(e.EducationGraduationYear, 1, 1, 0, 0, 0),
})
.ToListAsync().ConfigureAwait(false);
在某些情況下,「e.EducationEntryYear」爲0,結果返回此異常Conversion failed when converting date and/or time from character string.
什麼是有第一個參數作爲本年度最簡單的方法,如果e.EducationEntryYear
是0
替換它與'e.EducationEntryYear == 0只需更換'e.EducationEntryYear'? DateTime.Now.Year:e.EducationEntryYear' – itsme86
你有沒有試過e.EducationEntryYear> 0?e.EducationEntryYear:2016? –
您必須先將DateTime.Now.Year保存到變量中 –