Refactored foreach loop to linq query