有一些方法可以做到這一點:
// this way would throw exception if no result was found or more than 1 result
EMP e =(EMP) db.EMPs.Single(n => n.id == j);
// if no result was found, return null
EMP e =(EMP) db.EMPs.SingleOrDefault(n => n.id == j);
// if no result was found, throw exception.
// if there are more than 1 result, return the first item
EMP e =(EMP) db.EMPs.First(n => n.id == j);
// if no result was found, return null.
// if there are more than 1 result, return the first item
EMP e =(EMP) db.EMPs.FirstOrDefault(n => n.id == j);
// cast to list
List<EMP> e =(EMP) db.EMPs.Where(n => n.id == j).ToList();
// then, select the first item via index:
return View(e[0]);
附加說明:請務必檢查FirstOrDefault'的'的返回值,因爲它也可以的情況下,謂語不匹配返回'null'。 – ckruczek
謝謝,它的工作原理 – Saurabh