Mercedes E320 air conditioning problem

The air conditioning only works some of the time. When I start the car it usually works for a few minutes then blows warm air. If I shut the car off and restart it the air will sometimes work properly. The shut off and restart may have to happen several times before the air conditioning will stay blowing cold air. Any suggestions on what is happening I’m confused.