Do all the queries outside of the loop, then force them to happen (convert to list) before looping.