2012-08-03 52 views
1

我遇到了將SQL從存儲過程轉換爲LINQ到SQL代碼的問題。查詢基本上對項目進行分組並返回數字值的總和。試圖將SQL查詢轉換爲LINQ到SQL

我一直試圖做的代碼是執行,但它不分組項目,總和也不起作用。我只是得到了一些價值,比如我沒有任何團隊,也沒有總和。

這似乎也沒有問候我希望它從中選擇的組織。

Sql代碼是有效的,LINQ到SQL轉換是一個問題。

如果有人能指出我做錯了什麼,我將不勝感激。

SQL代碼:

CREATE TABLE #T1 (a varchar(255), b varchar(255), c varchar(250), d varchar(250), e varchar(250), ACAP int); 



INSERT INTO #T1 
Select SubProgram AS a, SubSubProgram as b, SubSubSubProgram as c, Sub4Program as d, Su5Program as e, Sum(Deliverable1) AS ACAP 
FROM LogicModel.Deliverables, LogicModel.ProgramNumbers 
WHERE 
    LogicModel.ProgramNumbers.OrganizationName = @OrganizationName AND 
    LogicModel.ProgramNumbers.ProgramNumber = LogicModel.Deliverables.ProgramNumber AND 
    LogicModel.Deliverables.FiscalYear = @FiscalYear AND 
    LogicModel.Deliverables.Program = (09-10.1a) 'Education presentations by type' 
GROUP BY 
    SubProgram, SubSubProgram, SubSubSubProgram , Sub4Program, Sub5Program 


SELECT a, b, c, d, e, isnull(ACAP, 0) as ACAP from #T1 

Sql的Linq

var deliverables = (from de in OCHART.Deliverables 
           join pn in OCHART.ProgramNumbers on de.ProgramNumber equals pn.ProgramNumber1 into prt 
           from x in prt.Where(prt2 => prt2.OrganizationName.Equals(organization)).DefaultIfEmpty() 
           where de.FiscalYear == FiscalYear && de.Program.Equals("(09-10.1a) Education presentations by type") 
           group de by new { SubProgram = de.SubProgram, SubSubProgram = de.SubSubProgram, SubSubSubProgram = de.SubSubSubProgram, Sub4Program = de.Sub4Program, Sub5Program = de.Sub5Program, deliverable = de.Deliverable1 } into gr 
           select new 
           { 
            SubProgram = gr.Key.SubProgram, 
            SubSubProgram = gr.Key.SubSubProgram, 
            SubSubSubProgram = gr.Key.SubSubSubProgram, 
            Sub4Program = gr.Key.Sub4Program, 
            Sub5Program = gr.Key.Sub5Program, 
            deliverable1 = gr.Sum(g => g.Deliverable1), 
            deliverable2 = gr.Sum(g => g.Deliverable2) 
           }).ToList(); 

感謝您抽出寶貴時間,並試圖幫助。我很感激。

+0

我把它映射存儲過程和射擊從ef dbcontext不是一個選項? – 2012-08-03 02:56:39

+0

這是一個選項,但我仍然想知道如何使用linq to sql來完成,因爲我花了很多時間。爲了我個人的教育和學習。 – Dmitris 2012-08-03 03:11:58

+0

也許這是一個錯字,但'(09-10.1a)'按類型教育演示'!='「(09-10.1a)按類型教育演示文稿'' – kaveman 2012-08-03 03:16:51

回答

1

快速的解決方案(解決方法):

你爲什麼不跑你的存儲過程。由於您的存儲過程已經過測試。這會節省你的時間。

只需將存儲過程拖放到您的DBML文件即可。你所要做的就是使用它。