Conditional Expectation : How is E[E[xy|x]]=E[xE[y|x]]?

I read that "The law of total expectations states E[xy]=E[E[xy|x]]. By linearity of conditional expectations, E[E[xy|x]]=E[xE[y|x]]" but I am not able to understand the part "E[E[xy|x]]=E[xE[y|x]]"

I would be really thankful if you could explain this.

To supplement the other answers by Taylor and mhdadk, I find it has never led me astray to have absolute clarity on the probability distributions with respect to which one is computing expectations.

And if the properties being used by the other answers are unfamiliar, then it can be useful to return to the original definition of expectation and conditional expectation, and to see how these properties are merely abbreviations for what is going on with the integral.

In the continuous case, i.e. assuming that $$X, Y \in \mathbb{R}$$, we have a marginal density $$f_X(x)$$, conditional density $$f_{Y | X}(y | x)$$, and a joint density $$f_{X, Y}(x,y)$$. Omitting the range of integration $$(-\infty, \infty)$$ as well as formal technicalities, we have that

\begin{align} \mathbb{E}_{X, Y}[XY] &= \iint xy f_{X, Y}(x, y) \space dy \space dx \\ &= \iint xy f_{Y|X}(y|x) f_X(x) \space dy \space dx \\ &= \int f_X(x) \int xy f_{Y|X}(y|x) \space dy \space dx \space (= \mathbb{E}_X[\mathbb{E}_{Y|X}[XY \mid X]])\\ &= \int xf_X(x) \int y f_{Y|X}(y|x) \space dy \space dx\\ &= \mathbb{E}_{X} [X \space \mathbb{E}_{Y|X}[Y \mid X]] \end{align}

From 1st to 2nd equality, I have the used fact that $$f_{X, Y} = f_X \cdot f_{Y|X}$$. From 2nd to 4th equality, both $$f_X$$ and $$x$$ can be factored out of the inner integral, because the inner integral is with respect to $$y$$. The final equality follows from the definitions. All integrals can be replaced with summations in the discrete case, which Xi'an has covered more generally and with a greater degree of rigour in the following thread:

Conditional expectation: an implication

You can reduce this question to this: why is $$E[XY|X] = XE[Y|X]$$ (with probability 1)? If this is true, then you can just take expectations on both sides.

The answer is "because of the (technical) definition of conditional expectation." Thinking of it like the linearity property of nonconditional expectations can be helpful, too. If it is, stick with that.

\begin{align} E[E[XY|X=x]] &= E[E[xY|X=x]] \\ &= E[xE[Y|X=x]] \end{align} In the first line, $$X$$ in the inner expectation is assumed to be constant, so it is replaced accordingly. In the second line, $$E[aX] = aE[X]$$ for any constant $$a$$ by the linearity of the expectation operator.