Hazrat Moulana Ashraf ‘Ali Thanwi (rahmatullahi ‘alaih) once mentioned:
In order for one to benefit in Deen, one is required to associate with the pious and learn Deen. Completely disassociating oneself and remaining aloof from people is discouraged as it will be a means for harm coming to one’s Deen. However, in situations where disassociating oneself from certain classes of people is beneficial for one’s Deen, then even though it may be harmful to one’s dunya, it will not be discouraged, as it will be a means of safeguarding one’s Deen. (Malfoozaat Hakeemul Ummat 23/59-60)
Source: Ihyaauddeen.co.za