Consider a measure space $(X, \mathscr{X}, \mu)$ and a function $f:X \to \mathbf{R}$ which is Borel measurable (relative to $\mathscr{X}$). Consider the following function $\nu$ defined on the Borel sets of $\mathbf{R}$
$$
\nu (B) = \mu(f^{-1}(B)),
$$
where $f^{-1}(B)$ is the preimage of $B$ by $f.$ This $\nu$ can be shown to be a measure on the Borel sets of $\mathbf{R}$ which is therefore called the image measure of $\mu$ by $f$ and denoted $f(\mu).$
Suppose now that we have a probability space $(\Omega, \mathscr{F}, \mathbf{P})$ and a random variable $X$ (which by definition is a Borel measurable real-valued function). The image measure of $\mathbf{P}$ by $X$ is known as the distribution law of $X$ and, by measure theoretic arguments, this measure is univocally identified with a function $F:\mathbf{R} \to [0, 1]$ such that $F$ is non-decreasing, right-continuous, with left-limits and $F(-\infty) = 0,$ $F(\infty) = 1,$ we call graciously this $F$ as a distribution function. By definition then
$$
\mathbf{E}(\mathbf{1}_B(X)) = \mathbf{P}(X \in B) = \int_B dF(x) = \int_\mathbf{R} \mathbf{1}_B(x) dF(x),
$$
where $dF$ now denotes the distribution law of $X$ (defined on the Borel sets of $\mathbf{R}$). Using measure theoretic arguments (linearity and monotone classes of functions), we can show that
$$
\mathbf{E}(u(X)) = \int_\mathbf{R} u(x) dF(x),
$$
for any function $u:\mathbf{R} \to \mathbf{R}$ that is Borel measurable (in the sense that either both integrals exist and are equal, or neither of them exist).
Sometimes it is the case that $dF$ has a density relative to Lebesgue measure, this means that there exists a Borel function $f:\mathbf{R} \to \mathbf{R}_+$ such that
$$
dF(B) = \int_B f(x) dx.
$$
In this case, it can be shown that
$$
\int_\mathbf{R} u(x) dF(x) = \int_\mathbf{R} u(x) f(x) dx.
$$
Putting all of these together, you get what you were asking, the expected value of $X$ is
$$
\mathbf{E}(X) = \int_\mathbf{R} x f(x) dx.
$$
$$\int Xd\mathbb{P}(X\le x) = \int XdF_X(x) = \int xf(x)dx $$ where $F$ is the CDF for the distribution of $X$, and we have $F' = f$. The integral $\int Xd\mathbb{P}$ is the Riemann–Stieltjes integral. It is also a more general form because perhaps there is no density function for the distribution $X$ comes from.
– oliverjones Aug 31 '22 at 17:21