This is taken from the book "A First Course in Probability" from Sheldon Ross:
Sometimes a situation arises in which the value of a random variable $X$ is observed and then, on the basis of the observed value, an attempt is made to predict the value of a second random variable $Y$. Let $g(X)$ denote the predictor; that is, if $X$ is observed to equal $x$, then $g(x)$ is our prediction for the value of $Y$. Clearly, we would like to choose $g$ so that $g(X)$ tends to be close to $Y$. One possible criterion for closeness is to choose $g$ so as to minimize $E[(Y − g(X))^{2}]$. We now show that, under this criterion, the best possible predictor of $Y$ is $g(X) = E[Y|X]$.
So, I understand most of the proof that is provided in the book, but the start of the proof is what I do not understand:
So, why is ok to write the conditional expectation? We are trying to minimize the unconditional expectation, why can we assume that is the same as trying to minimize the conditional expectation on $X$?