How to dial from an outside line directly to an extension

If you want to dial through to an extension directly without going through an IVR, sure, you need to get those numbers from your voice service provider and pay for each one.

If you are interested in a large number of DIDs, you can get them for maybe 10-15 cents each per month, but that pricing is usually only available from the big Telco companies/ ISPs that also own the wiring infrastructure for your internet. Some resellers have that pricing too.

DIDs were cheap in the PRI days, but some companies still have pricing models available that resemble that of a PRI, although they no longer sell that technology. They have moved to SIP.

Spectrum (former TWC) e.g. charges 15c per DID per month in the NY area.
I know other resellers that charge 10c.

As far as the configuration on your PBX goes, you have two options:
Use an inbound route for each DID,
https://wiki.freepbx.org/plugins/servlet/mobile?contentId=28182102#content/view/28182102

or, if you can always match the last digits of your DID to an extension, do this: