In the Peter Jackson movie adaptations of both books, Gandalf was played by Ian McKellen. In The Hobbit movies, which were written as a prequel to The Lord of the Rings movies, he figures out that the Necromancer happens to be Sauron.