A new formula for some linear stochastic equations with applications