# Generating functions of discrete random variable

I am trying to understand the solution of a problem.

$X_1,X_2,....$ a sequence of independents randoms variables and same probability distribution.

$N$ rv. taking its values in $\mathbf{N}$

Considering $Z=\sum_{i=i}^N X_i$

We have : $$G_Z(s)=E(s^Z)=E(s^{\sum_{i=0}^N X_i})=\sum_kE(s^{\sum_{i=0}^N X_i}|N=k)P(N=k)$$

I don't understand how can we pass from $E(s^{\sum_{i=0}^N X_i})$ to that $\sum_kE(s^{\sum_{i=0}^N X_i}|N=k)P(N=k)$

-

For every measurable partition $(A_n)_{n\geqslant0}$ of the space $\Omega$ such that $\mathrm P(A_n)\ne0$ for every $n\geqslant0$, and every integrable random variable $Y$, $$\mathrm E(Y)=\sum_{n=0}^{+\infty}\mathrm E(Y:A_n)=\sum_{n=0}^{+\infty}\mathrm E(Y\mid A_n)\cdot\mathrm P(A_n).$$ Apply this to $Y=s^Z$ and $A_n=[N=n]$.