简体   繁体   中英

SQL Server Date to C# DateTime

I'm new to using SQL Server 2012 and the Date type in my C# applications and am curious about the conversion to DateTime . I have a DBML file representing my table and a type that has a server data type of Date and a type of DateTime . I'm trying to do a simple comparison between the current day and the value in my table:

if (adc.HolidaySchedules.FirstOrDefault(d => ((DateTime)d.HolidayDate).Date.AddDays(-1) == DateTime.Today.Date) != null)

It throws the following SqlException

The datepart millisecond is not supported by date function dateadd for data type date.

I'm curious as to why I'm getting this particular exception when I'm only accessing the Date and what I should be doing, explicitly or otherwise, to handle this type of conversion. I would have thought I could call AddDays() on a Date .

Why are you transforming each value in column HolidayDate instead of passing the correct parameter?

var date = DateTime.Today.AddDays(1);
var item = adc.HolidaySchedules.FirstOrDefault(d => d.HolidayDate == date);
if(item != null)
{
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM