I want to create a Dictionary where the Keys are representing elements from a List. Each list element has to be present in this dictionary.
Example:
A movie has a list of actors.
Now I want a list of movies the actor acted in. If the movies have more than one actor, each actor should have the movie in their own list.
I have a data strucuture like this:
public class MovieData
{
public List<string> Actors { get; set;}
public string Title { get; set; }
public DateTime ReleaseDate { get; set; }
public string FilePath { get; set; }
}
A small example to illustrate this:
var movie1 = new MovieData {
Title = "Movie1",
Actors = new List<string> { "Bob Smith", "Alice Stevens"}};
var movie2 = new MovieData {
Title = "Movie2",
Actors = new List<string> { "Alice Stevens"}};
var movie3 = new MovieData {
Title = "Movie3",
Actors = new List<string> { "John Doe" }};
The desired result should look like this:
Alice Stevens => { "Movie1", "Movie2" }
Bob Smith => { "Movie1" }
John Doe => { "Movie3" }
I came up with the following code, but it only pays attention to the first actor in the list (FirstOrDefault()). So other actors in the list are ignored.
var actorMovieList = movieDataList
.ToLookup(movie => movie
.Actors
.FirstOrDefault(), movie => movie);
How can I include the other actors in the list? I have to iterate through the actor list inside of the lambda, but I don't know how.
