我正在使用實體框架來訪問我的數據庫,我剛纔發現將存儲過程映射到使用實體的方法有點太複雜,可能不太適合做我想要的東西。我是LINQ的新手,我想知道是否應該用ADO.NET更好。下面是SQL代碼,我需要翻譯:LINQ to Entities而不是存儲過程?
SELECT p.Player_id, p.Name, p.Position, SUM(s.Goals) AS goalsb, SUM(s.Assists) AS assistsb, SUM(s.Points) AS pointsb
FROM Dim_Player AS p INNER JOIN Fact_Statistics AS s ON s.Player_id = p.Player_id
GROUP BY p.Player_id, p.Name, p.Position
ORDER BY pointsb DESC, goalsb DESC
隨着實體映射存儲過程,我有這個代碼寫的:
HockeyStatsEntities db = new HockeyStatsEntities();
public ActionResult Index()
{
ViewBag.Message = "League leaders";
{
return View(db.ListLeagueLeaders());
}
}
public ActionResult About()
{
return View();
}
private ICollection<ListLeagueLeaders_Result> ListLeagueLeaders()
{
ICollection<ListLeagueLeaders_Result> leagueLeadersCollection = null;
using (HockeyStatsEntities context = new HockeyStatsEntities())
{
foreach (ListLeagueLeaders_Result leagueLeader in
context.ListLeagueLeaders())
{
leagueLeadersCollection.Add(leagueLeader);
}
}
return leagueLeadersCollection;
}
這裏是我得到的錯誤:
傳遞到字典中的模型項目類型爲 'System.Data.Objects.ObjectResult`1 [NHLStats2.Models.ListLeagueLeaders_Result]', ,但該字典需要模型ite m類型爲 'NHLStats2.Models.ListLeagueLeaders_Result'。
但是我意識到,這是一個有點痛苦的? & * @ ...我如何使用不同的更有效的方法重新安排此操作?感謝您的幫助,非常感謝。