Yehia A.Salam
Yehia A.Salam

Reputation: 2028

Map the EF Object to the ViewModel Class without anonymous types

Is there a shorter version to map the EF from the linq or lambda expression to the View Model (Movie here is the ViewModel), knowing that the EF and the ViewModel have the same fields(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);
        }


    }

Upvotes: 1

Views: 194

Answers (1)

podiluska
podiluska

Reputation: 51504

You're looking for a mapping framework - such as AutoMapper. http://automapper.org/

Upvotes: 2

Related Questions