Getting error in MVC Proj when writing a Lambda expression

I am creating an example application (MVC). I did a great job of viewing and creating a new record, but when I wrote the code to get information about a particular record, I met 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 to get the info

public ActionResult Details(int id)
{
    var moviedetail = (Movie)_entities.MovieSet.Where(mvid => mvid.Id == id);
return View(moviedetail);
}

can any body tell me what is going wrong and where?

thank.

+3
source share
3 answers

- Where IEnumerable, Movie. . Where extension, . , , Movie, First(), .

public ActionResult Details(int id) 
{ 
    var moviedetail = _entities.MovieSet.Where(mvid => mvid.Id == id).First(); 
    return View(moviedetail); 
} 
+1

var moviedetail = (Movie)_entities.MovieSet.FirstOrDefault(mvid => mvid.Id == id);

Where , ToList(), , id, , , First, , match, FirstOrDefault (, null), .

+1

, . . , . Single() Where() First().

var moviedetail = (Movie) _entities.MovieSet.Single (mvid => mvid.Id == id);

0
source

Source: https://habr.com/ru/post/1744144/


All Articles