开发者

EntityCollection OrderBy problem

开发者 https://www.devze.com 2023-02-05 21:29 出处:网络
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)).ToL

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

精彩评论

暂无评论...
验证码 换一张
取 消