tags:

views:

272

answers:

1

Hi all, I have some problems in executing a Linq query with the group by clause in a C# project. Here following the query:

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

I hope the query will be printed out in the page in a better style than I am seeing at the moment :(. Anyway I need to get in the constructor of ControlFormReportPartialResults all the fileds I have selected in the group by clause, but it gives me an error type expected.

Any help is really appreciated.

Regards,

Dario

+3  A: 

Assuming that the rest of the query is correct, your issue is just a syntax problem in your group by clause (specifically using the new operator to create an anonymous type). You're using parens, but curly braces should be used. This should do the trick:

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)
 );
Adam Robinson
+1 Sharp eyes you've got!
Abel