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)