1

我在将 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

  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();

感谢您抽出时间并尝试提供帮助。我很感激。

4

1 回答 1

1

快速解决方案(解决方法):

你为什么不运行你拥有的存储过程。由于您的存储过程已经过测试。这会节省你的时间。

只需将存储过程拖放到您的 DBML 文件中即可。您所要做的就是使用它。

于 2012-08-09T06:47:54.810 回答