I have read a few items on this, including How to select only the records with the highest date in LINQ but I don't know how to apply it to my case which is slightly more complex.
I am trying to get all AdjusterProfileStatusItem
but only select the most recent s.statusDate
. Currently, the query just returns all dates for all records; whereas I just want the most recent date for all records.
(from u in db.Users
join a in db.Adjusters
on u.id equals a.userID
join s in db.AdminAdjusterStatus
on a.id equals s.adjusterID
where u.userType.ToLower() == "adjuster"
&& s.status.ToLower() == "approved"
&& s.statusDate.Max() // causes syntax error...
select new AdjusterProfileStatusItem
{
user = u,
adjuster = a
})
Edit:
I have also tried this which gives me a syntax error...
(from u in db.Users
join a in db.Adjusters
on u.id equals a.userID
join s in db.AdminAdjusterStatus
on a.id equals s.adjusterID
where u.userType.ToLower() == "adjuster"
&& s.status.ToLower() == "approved"
group new { u, a, s } by s.adjusterID into x
select new AdjusterProfileStatusItem
{
user = u, // u does not exist in context
adjuster = a, // a does not exist in context
status = x.Max(y => y.statusDate) // anonymous type does not contain definition for 'statusDate'
})