2010-12-20 222 views
0

我有一個正在運行的sql查詢,在過去的幾天裏我一直試圖轉換爲linq。我已經在這裏和各個網站上閱讀了幾篇文章,但是在我嘗試將其轉換爲工作linq查詢的嘗試中都沒有成功。如何將sql查詢轉換爲linq

我特別在處理數學方面遇到問題的部分(添加單個分數以獲取total_score值)並從Schools表中獲取值,以便我可以將它們傳遞給我的視圖。

這裏是SQL查詢:

SELECT bowler_name, school_name, bowler_gm1, bowler_gm2, bowler_gm3, bowler_gm4, bowler_gm5, bowler_gm6, (ISNULL([bowler_gm1],0)+ISNULL([bowler_gm2],0)+ISNULL([bowler_gm3],0)+ISNULL([bowler_gm4],0)+ISNULL([bowler_gm5],0)+ISNULL([bowler_gm6],0)) AS total_score 
FROM Bowlers 
JOIN Schools ON Schools.school_id = Bowlers.school_id 
ORDER BY total_score DESC; 

這是據我得到與LINQ查詢事情開始打破之前:

from s in db.Schools 
join b in db.Bowlers on s.school_id equals b.school_id 
select b; 

任何幫助將非常感激。謝謝!

回答

1

像這樣的東西應該可以幫助你。

from s in db.Schools 
join b in db.Bowlers on s.school_id equals b.school_id 
select new { 
    bowler_name = b.bowler_name, 
    ... 
    totalscore = bowler_gm1 + bowler_gm2 + bowler_gm3 ... 
}; 

注意,如果你要創建一個BowlerGame表 遊戲ID爲PK,BowlerID爲FK到投球手,GameNumber作爲遊戲人數和最終得分爲那場比賽的比分會更容易處理空值

+0

感謝您的幫助 - 我把它放在一起,現在只是努力獲取視圖。謝謝! – chemqueen 2010-12-20 22:24:35