Extract linq statements from foreach