What is wrong with the following code?
List students = new List();
students = db.Students.Where(c => c.StudentCourses开发者_StackOverflow.OrderBy(o => o.EnrolledTo > DateTime.Now.Date)).ToList();
I am getting error: delegate does not take 1 arguments.
Thanks
I guessing a bit here, but I think that you are trying to get the latest date from each student and compare it to todays date. If you do it using OrderBy
you would need a Last
call to get the single value, but you should rather just use Max
.
Note: Don't use DateTime.Now
in a loop, as the value changes. If you run the query at midnight it may change in the middle of the loop, and you get an inconsistent result. Put the value in a variable before the loop.
DateTime today = DateTime.Today;
students = db.Students
.Where(c => c.StudentCourses.Max(o => o.EnrolledTo) > today)
.ToList();
精彩评论