Problem regarding mean values of variations in statistical mechanics

Take a look at this previous question; in the answer by Julian Helfferich we arrive at the following formula for an infinitesimal change of entropy: $$dS = \frac{1}{T}\sum_i d(E_ip_i) - d(E_i)p_i$$ all this is completely fine for me, no problem here. But then comes the crucial point: we can state that $$\sum _i d(E_i p_i)$$ is the expectation value of the change of the energy of the system and also we can say that $$\sum _i d(E_i) p_i$$ is the expectation value of the work done on the system. Using those statements, and also the first principle of thermodynamics, we can immediately show that: $$dS=\frac{dQ}{T}$$ that indeed is what we are trying to show.

I can see roughly that this two statements are true, but I would like a formal mathematical proof of those; for example: regarding the first statement we can surely say that: $$\langle E \rangle= \sum _i E_i p_i$$ and so we can say that the variation of the expectation value is: $$\sum _i d(E_ip_i)$$ seems easy right? But here comes the problem: is the variation of the expectation value equal to the expectation value of the variation? Is this the right way to show that $$\sum _i d(E_ip_i)=dE$$?
But most importantly: How can we show that $$\sum _i d(E_i)p_i=dW$$? How can we prove this? I have also tried to read the related wikipedia page, but the explanation there does not convince me: the mathematical derivation is not reported there.

What expectation values do we consider? The system energy $$\langle E\rangle$$ is the expectation value of its Hamiltonian over the given statistical ensemble. In classical physics the ensemble is defined by a set of probabilities $$\{p_i\}$$ to find the system in $$i$$-th microstates with energies $$E_i$$, so that $$\langle E\rangle=\sum_iE_ip_i$$. In quantum physics we average the Hamiltonian over the density matrix $$\rho$$ of the system, so $$\langle E\rangle=\mathrm{Tr}\,[H\rho]$$.
The variation of energy $$dE$$ is considered when something happens with our system under external influence: generally, the system Hamiltonian can change due to action of external forces (change of volume, application of electric or magnetic fields etc.) and, at the same time, the system itself can redistribute over microstates, i.e. change $$\{p_i\}$$ due to heat taken from environment (usually in a form of random collisions between system molecules and atoms of the walls, which redistribute momenta of the former ones).
In variation of $$\langle E\rangle$$ we take into account both variation of the quantity we average and variation of probability distribution over which the averaging is performed. So no, variation of the expectation value is not generally equal to expectation value of variation, because the probability distribution can also change. In classical setting we have $$dE=d\left(\sum_iE_ip_i\right)=\sum_i(dE_i)p_i+\sum_iE_i(dp_i)$$ (for infinitesimal variations) and in quantum setting we have $$dE=d(\mathrm{Tr}\,[H\rho])=\mathrm{Tr}\,[(dH)\rho]+\mathrm{Tr}\,[H(d\rho)].$$ The first term in both these expressions is the expectation value of variation of the Hamiltonian and it is attributed to work $$\delta W$$. The second term is the heat $$\delta Q$$ taken by the system.
Now: how we can prove that $$\delta W=\sum_i(dE_i)p_i$$? I'm not sure that some rigorous proof exists, eventually, we can just use this expression as definition of work. As far as I know, it is routinely done in development and justification of thermodynamics starting from microscopic classical or quantum physics.
This definition agrees with our common-sense understanding what the work in thermodynamics is: change of system energy induced by deterministic, well-controlled changes of its parameters (presumably volume, but electromagnetic fields or electric voltages are also used). In microscopic language it means we change parameters of the system Hamiltonian $$H$$ or its energy levels $$E_i$$. The work is opposed to heat which represents changes of system energy due to external action on degrees of freedom we do not control explicitly (such as random molecular collisions which change $$\{p_i\}$$, but not $$\{E_i\}$$).