GroupBy Linq IEnumerable error

192 views Asked by At

I am trying to do a Group By on an IEnumerable. However whenever I try to complete, I keep getting an error.

Dim query As IEnumerable(Of CreditItem) = _
    From credits In unprocessedCredits _
    Group credits By unprocessedCreditsGrouped = _
           New With {Key .Make = credits.Make, _ 
                     Key .Part = credits.PartNumber, _
                     Key .CustomerNumber = credits.CustomerNumber, _ 
                     Key .OrderNumber = credits.OrderNumber} _
    Into Group _
                    Select New CreditItem With { _
                    .Make = unprocessedCreditsGrouped.Make, _
                    .Part = unprocessedCreditsGrouped.PartNumber, _
                    .CustomerNumber = unprocessedCreditsGrouped.CustomerNumber, _
                    .OrderNumber = unprocessedCreditsGrouped.OrderNumber _
                    })

I need to Group By multiple properties, for example by make, partnumber, customernumber. Also unprocessedCredits also contains the field "quantity" which i will need to get the sum of quantity based on the group by clause. Select New CreditItem is not working. getting some Argument not specified for specified parameter.

However, I don't know if i really need the select statement because i really want to select everything in this ienumerable.

Not just those properties, but i don't think that's possible with group by?

1

There are 1 answers

1
Gert Arnold On

You can create a CreditItem directly as grouping variable:

Dim query As IEnumerable(Of CreditItem) = _
    From credits In unprocessedCredits _
    Group credits By unprocessedCreditsGrouped = _
           New CreditItem With _
                         { _
                             .Make = credits.Make, _ 
                             .Part = credits.PartNumber, _
                             .CustomerNumber = credits.CustomerNumber, _ 
                             .OrderNumber = credits.OrderNumber _
                         } _
    Into Group _
    Select unprocessedCreditsGrouped