Why are you even looping over all the products in the first place? If you have product x and need to associate it with y, why are you not just adding the handle of y to x (via tag, metafield, whatever)? Or just checking to see if x.handle + ' sample' results in a valid y?
↧