get date part only from datetime value using entity framework

前端 未结 3 1708
时光取名叫无心
时光取名叫无心 2020-12-03 18:00

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         


        
3条回答
  •  盖世英雄少女心
    2020-12-03 18:13

    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);
    

提交回复
热议问题