1

是否有更短的版本可以将 EF 从 linq 或 lambda 表达式映射到 View Model(这里的Movie是 ViewModel),知道 EF 和 ViewModel 具有相同的字段(Id、Director、Plot...)?

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);
        }


    }
4

1 回答 1

2

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

于 2012-09-03T14:57:09.620 回答