Chapter 2: Ticket Projection two approaches with different result

I have created two ways to query the requested movies by country:

Initially my approach was the following:

As stated in the second Assertions, when Russia and Japan passed, the resulted movie collection should have 1237 records, however my result was 1245. However the integration test passed and I got a correct validation code.

After some thought I noticed that I have used .ToList() twice which isn’t very optimized.

So I used my second approach

The resulted movie collection had 1237 records.

My question is why the two approaches produced different result? any ideas will be well appreciated.

Thank you in advance.

1 Like

Probably by using the first approach you are adding twice same movies. Let say if a movie has both countries.
Also it is enough to return movies. You don’t need to add sort and toList again on the last line.

 return await Task.Run(() =>
            var movies = new List<MovieByCountryProjection>();
            var filter = Builders<Movie>.Filter.In("countries", countries);
            var sortByDesc = Builders<Movie>.Sort.Descending(m => m.Title);

            .Project(m => new MovieByCountryProjection { Title = m.Title, Id = new ObjectId(m.Id) })
            .ForEach(m => movies.Add(m));

            return movies;