Halid
Halid

Reputation: 448

Whats wrong in this Linq query

var MyCours = Db.COURS.Where(C => C.CLASSE_ID == ClassID
                                        && DateTime.Now>= C.START_DATE
                                        && DateTime.Now <= C.END_DATE)
         .ToList();

Some change still dont work !

Upvotes: 0

Views: 61

Answers (1)

StuartLC
StuartLC

Reputation: 107267

A likely problem is that the provider can't project DateTime.Compare into a SQL statement. There is potentially also a logical error in the direction of comparison (unless you really want enddate < now < startdate), and I would also suggest using .ToList() to materialize into a list:

var theTimeNow = DateTime.Now;
var MyCours = Db.COURS.Where(C => C.CLASSE_ID == ClassID
                                            && theTimeNow >= C.START_DATE
                                            && theTimeNow <= C.END_DATE)
             .ToList();

Projecting DateTime.Now into a variable isolates the non-determinism of it, i.e. to ensure that both comparisons are against the same time.

Upvotes: 4

Related Questions