Suppose we have a canonical ensemble system of $N$ particles, and $k$ single-particle-energy levels labeled by $\epsilon_i$.
The energy of the different microstates of the entire system is given by $\sum n_i\epsilon_i$, such that $\sum n_i = N.$
Let us label the total energies of the system using $E_i$ as opposed to the single particle energy levels.
The total expectation value of energy is defined as :
$$\langle E\rangle=\sum_{i}^{m}P(E_i)E_i$$
Here $E_i$ represents the total energy of a microstate, and $P(E_i)$ represents the probability of the $N$ particle microstate. The summation goes till $m$ which is the total number of $N$ particle microstates of the entire system as a whole.
However, I've seen some sources, like this for example, describe the expectation value of energy in a slightly different way.
According to this source, $$\langle E\rangle=\sum_i^k n_i\epsilon_i$$
In this case, $n_i$ represents the (expected) number of particles in the $\epsilon_i$ energy level. In this case, the summation goes till $k$ which is the number of single particle energy states of the system.
These two definitions seem to be equivalent intuitively. For example, in the thermodynamic limit, $n_i$ is the exact number of particles occupying the $\epsilon_i$ energy level, and so $\langle E\rangle$ must be the total energy of the system. In the previous case, we know that as we reach the large number limit, the energy fluctuations die out, and the expected energy takes a particular value.
However, can someone help me show this mathematically that :
$$\langle E\rangle=\sum_i^k n_i\epsilon_i=\sum_{i}^{m}P(E_i)E_i$$
where ,$$n_i = NZ^{-1}e^{-\beta \epsilon_i}$$
In the first definition, the expected total energy is the energy of each microstate of the system multiplied by its probability and summed over. In the second definition, the expected total energy is the sum of the energies of each particle. This is done, by multiplying the energy of a single particle energy level, with all the particles expected to be in that level, and then summed over.