Chapter 2: Ticket: Projection

I am having a hard time with the code for this section. I am getting a type casting error and I am not sure what I am doing wrong.

        var moviesFilter= Builders<Movie>.Filter.In("countries", countries);


        var projectionFilter = Builders<Movie>.Projection
         .Include(m => m.Title)
         .Include(m => m.Id);

        return await _moviesCollection
            .Find<Movie>(moviesFilter)
            .Project<Movie>(projectionFilter)
            .SortByDescending(m => m.Title)
            .ToListAsync(cancellationToken);

I figured it out. The project is on the MovieByCountryProjection object. I had to change the code referencing the Project.

        var moviesFilter = Builders<Movie>.Filter.In("countries", countries);


        var projectionFilter = Builders<Movie>.Projection
         .Include(m => m.Id)
         .Include(m => m.Title);


        return await _moviesCollection
            .Find<Movie>(moviesFilter)
            .Project<MovieByCountryProjection>(projectionFilter)
            .SortByDescending(m => m.Title)
            .ToListAsync(cancellationToken);

This topic was automatically closed 5 days after the last reply. New replies are no longer allowed.