将EF对象映射到没有匿名类型的ViewModel类

时间:2012-09-03 14:55:29

标签: asp.net-mvc linq entity-framework

是否有更短的版本将EF从linq或lambda表达式映射到View模型( Movie 这里是ViewModel),知道EF和ViewModel具有相同的字段(Id ,导演,剧情......)?

public ActionResult MovieDetails(int ID) {


        using (MAKANI.Models.Entities db = new MAKANI.Models.Entities()) {


            var rMovie = (from m in db.Movies
                           where m.ID == ID
                           select new Movie {
                               Title = m.Title,
                               Director = m.Director,
                               Plot = m.Plot,
                               Link = m.Link,
                               Starring = m.Starring
                           }).Single();


            return View(rMovie);
        }


    }

1 个答案:

答案 0 :(得分:2)

您正在寻找一个映射框架 - 例如AutoMapper。 http://automapper.org/