Use Linq query to compare date only with DateTime field

后端 未结 7 1335
孤城傲影
孤城傲影 2020-12-20 14:59

I need to compare just the date only in a Linq query that involves a datetime field. However, the syntax below results in the following error message

<

7条回答
  •  臣服心动
    2020-12-20 15:20

    It might seem a little roundabout, but you can use the SqlFunctions class' DateDiff method for doing this. Just pass in both values and use "Day" for finding the difference between them in days (which should be 0 if they are on the same day).

    Like the following:

    from a in _db.AgentProductTraining
                    where a.CourseCode == course.CourseCode &&
                    SqlFunctions.DateDiff("DAY", a.DateTaken, course.DateTaken) == 0 &&
                    a.SymNumber == symNumber
                    select a;
    

提交回复
热议问题