如何通过使用的SelectMany到GROUPBY MVC视图结合 [英] how to pass selectmany combine with groupby to MVC view

查看:494
本文介绍了如何通过使用的SelectMany到GROUPBY MVC视图结合的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下的lambda前$ P $在MVC应用程序pssion。

i have the following lambda expression in MVC application.

 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;

但得到这个错误

Cannot implicitly convert type 'object' to 'System.Collections.Generic.IEnumerable<Movie>'. An explicit conversion exists (are you missing a cast?)

任何帮助将appriciated。

any help will be appriciated.

推荐答案

我会建议你做,描绘你的域实体(动画)像

i would recommend that you make a class that depicts your domain entity (Movie) like

public class MyMovie{
 public int movieID { get; set; }
 public string MovieTitle { get; set; }  
}

和修改的 LINQ 的查询作为

var toprating=  _db.Movie.SelectMany(m => m.Rating.Select(r=> new MyMovie
            {
              movieID=r.MovieID,
              MovieTitle= m.Title
            })).GroupBy(m=>m.movieID).ToList();

ViewBag.TopMovie = toprating;
IEnumerable<MyMovie> TopMovies = ViewBag.TopMovie; 

我认为电影是你的域实体,使用视图模型传授给的意见。

i have assumed that the Movie is your Domain entity, use view models to pass on to the views.

您可能会发现<一个href=\"http://stackoverflow.com/questions/901854/linq-how-to-declare-ienumerableanonymoustype\">this有益

you may find this helpful

这篇关于如何通过使用的SelectMany到GROUPBY MVC视图结合的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆