If you order the loans by LoanDate descending and then group by EquipmentId
, the first element in each group will be the most recent loan for its EquipmentId
. If the userId
is not null, then that is the ID of the user who currently has the equipment.
var items = from loan in loans
orderby loan.LoanDate desc
group loan by loan.EquipmentId into grp
let userId = grp.First().ToUser
where userId != null
select new { EquipmentId = grp.Key, UserId = userId };