Lets say I have a list of movies with a set of start times
public class Show
{
public string Name { get; set; }
public DateTime Start { get; set; }
}
Movie 1 - 8:00
Movie 1 - 10:00 Movie 1 - 12:00 Movie 2 - 9:00 Movie 2 - 11:30 Movie 2 - 15:00 Movie 3 - 12:00 Movie 3 - 13:00I end up with a grouped list, with the name of the movie being the key. I want to take one time from each group and create a List<DateTime>
.
Now since there are 3 items with 8 times, I should have 18 different lists.
8:00 - 9:00 - 12:00
8:00 - 9:00 - 13:00 10:00 - 9:00 - 12:00 etc.I tried just looping through the groups
foreach (var movie in movies)
{
foreach (var time in movie)
{
// Start looping through other groups and movies
}
}
Anyways, this is either a bad solution or I wasn't doing something right because I ended up with Lists of List<DateTime>
and I had to start looping through those lists to build more lists by only taking one item from each... It was just horrible. A regular nested foreach
won't work because then I end up with my 8:00 Movie 1 having all instances of Movie 2 & 3, so I had to keep the lists separate and that just became too messy.
Any suggestions of a better 开发者_StackOverflow中文版way to approach this?
Summary:
An easier way to understand it would be like this, you want to see 3 movies in a single day. You know which 3 you want to see, you don't know what times you want to see them at.
I need a list that would present all the options. You can see this movie at this time, this one at this time...
e.g.
List 1 - Movie 1 - 8:00, Movie 2 - 9:00, Movie 3 - 12:00
List 2 - Movie 1 - 8:00, Movie 2 - 9:00, Movie 3 - 13:00 List 3 - Movie 1 - 8:00, Movie 2 - 11:30, Movie 3 - 12:00 etc.Order of the list does not matter.
You're making this more complex than you need to I think. If you start with a list of showtimes, then it's a simple matter of ordering to enumerate.
List<Show> startTimes = GetTimesFromDB();
var moviesGrouped = startTimes.GroupBy(x => x.Name);
foreach(var group in moviesGrouped)
{
// print times for each movie
}
To use a concrete example, lets use this sample:
List<Sample> sample = new List<Sample>
{
new Sample { Id = 1, Time = DateTime.Now },
new Sample { Id = 1, Time = DateTime.Now.AddDays(1) },
new Sample { Id = 2, Time = DateTime.Now.AddDays(2) },
new Sample { Id = 2, Time = DateTime.Now.AddDays(3) },
new Sample { Id = 3, Time = DateTime.Now.AddDays(4) },
};
foreach (var group in sample.GroupBy(x => x.Id))
{
foreach (var element in group)
{
Console.WriteLine(element.Id);
Console.WriteLine(element.Time);
}
}
You simply group by your token (in this case movie name), and then enumerate the grouping (which will contain your movie times).
However, you could greatly simplify this by changing your object model.
public class Movie
{
public int MovieId { get; set; }
public string Name { get; set; }
public List<Showing> Showings { get; set; }
}
public class Showing
{
public DateTime StartTime { get; set; }
public List<Seat> UnsoldSeats { get; set; }
// Etc
}
Then when you have a List<Movie>
, it's already set up to do that display, and it naturally makes more sense than a simple showing class.
foreach(var movie in movies)
{
foreach(var showing in movie.Showings)
// DoStuff
}
精彩评论