So the real issue here is that you're executing a query for every single approval in every single record. You don't want to be doing that. What you want to be doing is joining your records on the database side with your approvals to create a query of pairs. Selecting out just the one matching approval from all of the joined approvals can be done with just a simple let
.
Once you've queried all of the approvals and the approval list from the record that pairs with it, updating each item is simple enough.
var query = from record in records
from recordApproval in record.approverList
join approval in approvals
on new { record.recordId, recordApproval.personId } equals
new { approval.recordId, approval.personId }
into approvalMatches
where approvalMatches.Any()
let approval = approvalMatches.OrderByDescending(a => a.modified)
.FirstOrDefault()
select new
{
recordApproval,
approval,
};
foreach (var result in query)
result.recordApproval.vote = result.approval.vote;
Additionally it's worth noting that you shouldn't materialize records
into a result set; you should keep it as an unmaterialized query so that the join can happen on the database site of things.