I can get the result just by taking average on both sides, and then getting the average inside the integral. Thats what the book does, but whats the justification for doing that?

This is what I mean:

If then I take that:

And:

I get the desired result from the given first moment:

But I would like to see a justification on those steps, i.e. how the first average gives the result it gives, and how the average can be taken inside the integral from the formal definition given for the average.