I don't know the original joke, but I assume it's a Chinese racism thing.
The answer is, I believe, that thousands of Chinese died building the railroads. It's the same sort of thing as why it's more offensive to joke about Jews burning or being gassed, than it is to joke about them being misers.