i am creating a sample movie (MVC) application. I was getting fine with Viewing and Creating a new record, but when i wrote the code to get the details of a particular record i met with the following error:
Unable to cast objec`t of type 'System.Data.Objects.ObjectQuery`1[MovieApp.Models.Movie]' to type 'MovieApp.Model`s.Movie'.
here is the code i wrote for getting the details
public ActionResult Details(int id)
{
var moviedetail = (Movie)_entities.MovieSet.Where(mvid => mvid.Id == id);
return View(moviedetail);
}
can any body tell me whats going wrong and where ??
thank you.
The problem in your code is the Where function returns you IEnumerable and you are typecasting it to Movie. Therefore it is failing. Check for syntax of Where extension function to see for yourself. So if you are sure that you will only be returned one Movie object, I suggest you use First() like this.