Hadithcore
Mishkat al-Masabih, 251

‘Ali reported God’s messenger as saying, “Good is the man learned in religion who, if people have need of him benefits them, but who, if they ignore him enriches himself.”* Razin transmitted it. *i.e. with religious exercises and study.

وَعَنْ عَلِيٍّ رَضِيَ اللَّهُ عَنْهُ قَالَ: قَالَ رَسُولُ اللَّهِ صَلَّى اللَّهُ عَلَيْهِ وَسَلَّمَ: «نِعْمَ الرَّجُلُ الْفَقِيهُ فِي الدِّينِ إِنِ احْتِيجَ إِلَيْهِ نَفَعَ وَإِنِ اسْتُغْنِيَ عَنْهُ أَغْنَى نَفْسَهُ» . رَوَاهُ رزين

References1 variant
In-Book Reference
Book 2, Hadith 48