Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I want to get date part only from database 'date time' value I m using the below code..but it is getting date and time part.

using (FEntities context = new FEntities())
{
    DateTime date = DateTime.Now;
    if (context.tblvalue.Any(x => x.date == date))
    {    
    }
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
358 views
Welcome To Ask or Share your Answers For Others

1 Answer

You can compare just specified parts:

context.tblvalue.Any(x => x.date.Year == data.Year 
                        && x.date.Month == data.Month 
                        && x.date.Day == data.Day);

EDIT: You can also try the following:

context.tblvalue.Any(x => EntityFunctions.TruncateTime(x.date) == data.Date);

Another approach:

context.tblvalue.Any(x =>  SqlFunctions.DatePart("year", x.date)  == data.Year 
                        && SqlFunctions.DatePart("month", x.date) == data.Month 
                        && SqlFunctions.DatePart("day", x.date)   == data.Day);

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...