我遵循本教程:http://www.asp.net/mvc/tutorials/mvc-music-store/mvc-music-store-part-4
作者使用Single方法:
public ActionResult Browse(string genre)//AUTHOR'S CODE
{
// Retrieve Genre and its Associated Albums from database
var genreModel = storeDB.Genres.Include("Albums").Single(g => g.Name == genre);
return View(genreModel);
}
和查找方法:
public ActionResult Details(int id)//AUTHOR'S CODE
{
var album = storeDB.Albums.Find(id);
return View(album);
}
通过类比Browse()
(第一个例子)方法,我想再次使用Single方法,所以我写道:
public ActionResult Details(int id)
{ //MY CODE
Album album = storeDB.Albums.Single(a => a.AlbumId == id);
return View(album);
}
问题:var album = storeDB.Albums.Find(id);
和Album album = storeDB.Albums.Single(a => a.AlbumId == id);
的工作方式有何不同?结果似乎是一样的。