i have the following lambda expression in MVC application.i want to group data.
var toprating= _db.Movie.SelectMany(m => m.Rating.Select(r=> new
{
movieID=r.MovieID,
MovieTitle= m.Title
})).GroupBy(m=>m.movieID).ToList();
ViewBag.TopMovie = toprating;
}
i want to pass this to my view.
i try writing the following in my view
IEnumerable<Movie> TopMovies = ViewBag.TopMovie;
but got this error
Cannot implicitly convert type 'object' to 'System.Collections.Generic.IEnumerable<Movie>'. An explicit conversion exists (are you missing a cast?)
any help will be appriciated.
The type of the collection that you are passing in isn’t an
IEnumerable<Movie>. Notice you are using an anonymous type, and then you’re also usingGroupBy. The resulting type is complex.Your query is also meaningless because you’re accessing
Ratingbut not using it, so you probably end up with multiple copies of the same movie, and with all the rating information discarded. For the moment I’ll assume you actually want the ratings included.So you could use the following query, for example:
Now the type of the above query is
List<IGrouping<Movie, Rating>>, so you can use that in your view: