【发布时间】:2011-03-16 23:08:28
【问题描述】:
我的控制器中有这段代码:
public ActionResult Details(int id)
{
using (var db = new MatchGamingEntities())
{
var MyMatches = from m in db.Matches
join n in db.MatchTypes on m.MatchTypeId equals n.MatchTypeId
where m.MatchId == id
select new MatchesViewModel
{
MatchType = n.MatchTypeName,
MatchId = m.MatchId,
MatchName = m.MatchTitle,
MatchDescription = m.MatchDescription,
Wager = m.Wager
};
ViewBag.MyMatches = MyMatches.ToList();
return View(MyMatches.ToList());
}
}
我希望能够使这个查询只返回一个结果,并且我可以使用 MyMatches 作为 MatchesViewModel 对象,因此我不必使用 ToList() 功能,从而摆脱视图页面上的 IEnumberable @987654323 @所以我可以把它变成这样:@model MatchGaming.Models.MatchesViewModel
【问题讨论】:
-
您可能正在寻找 .First()、.FirstOrDefault() 或 Single();而不是 .ToList()
标签: c# asp.net linq asp.net-mvc-3