在使用C#项目中的group by子句执行Linq查询时遇到一些问题。 以下是查询:
var items = (
from controlForm in dataContext.ControlForms
join controlFormStatus
in dataContext.ControlFormStatus
on controlForm.FK_ControlFormStatus equals controlFormStatus.Id
join docCheck
in dataContext.DocumentChecks
on controlForm.Id equals docCheck.FK_ControlForm
join checkResult
in dataContext.CheckResults
on docCheck.FK_CheckResult equals checkResult.Id
where controlForm.FK_ControlCycle.ToString().Equals(controlCycleId)
& controlFormStatus.Description.Equals(Constants)
| Description.Equals(Constants.ControlFormStatusApprovalProcessOwner))
group controlForm by
new (controlForm.Id,
controlForm.FK_ControlCycle,
controlForm.FK_SampleNode,
controlForm.FK_Control,
controlForm.TestExecutor,
controlForm.FK_ControlFormStatus)
into ctrlForm
select
new ControlFormReportPartialResults(
ctrlForm.Key.Id,
ctrlForm.Key.FK_ControlCycle,
ctrlForm.Key.FK_SampleNode,
ctrlForm.Key.FK_Control,
ctrlForm.Key.TestExecutor,
ctrlForm.Key.FK_ControlFormStatus ,
3 / 2)
);
我希望查询将以更好的风格打印在页面中,而不是我现在看到的:(。 无论如何,我需要在ControlFormReportPartialResults的构造函数中获取我在group by子句中选择的所有文件,但它给出了我预期的错误类型。
非常感谢任何帮助。
此致
的Dario
答案 0 :(得分:3)
假设查询的其余部分是正确的,那么您的问题只是group by
子句中的语法问题(特别是使用new
运算符来创建匿名类型)。你正在使用parens,但应该使用花括号。这应该可以解决问题:
var items = (
from controlForm in dataContext.ControlForms
join controlFormStatus
in dataContext.ControlFormStatus
on controlForm.FK_ControlFormStatus equals controlFormStatus.Id
join docCheck
in dataContext.DocumentChecks
on controlForm.Id equals docCheck.FK_ControlForm
join checkResult
in dataContext.CheckResults
on docCheck.FK_CheckResult equals checkResult.Id
where controlForm.FK_ControlCycle.ToString().Equals(controlCycleId)
& controlFormStatus.Description.Equals(Constants)
| Description.Equals(Constants.ControlFormStatusApprovalProcessOwner))
group controlForm by
new {controlForm.Id, /*updated*/
controlForm.FK_ControlCycle,
controlForm.FK_SampleNode,
controlForm.FK_Control,
controlForm.TestExecutor,
controlForm.FK_ControlFormStatus}
into ctrlForm
select
new ControlFormReportPartialResults(
ctrlForm.Key.Id,
ctrlForm.Key.FK_ControlCycle,
ctrlForm.Key.FK_SampleNode,
ctrlForm.Key.FK_Control,
ctrlForm.Key.TestExecutor,
ctrlForm.Key.FK_ControlFormStatus ,
3 / 2)
);