GroupBy Linq IEnumerable错误

时间:2015-06-18 21:19:55

标签: vb.net linq group-by ienumerable

我正在尝试在IEnumerable上执行Group By。但是,每当我尝试完成时,我都会收到错误。

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 _
                    })

我需要分组多个属性,例如make,partnumber,customernumber。此外,unprocessedCredits还包含字段" quantity"我需要根据group by子句得到数量之和。选择新的CreditItem不起作用。获取一些未指定参数的Argument。

但是,我不知道我是否真的需要select语句,因为我真的想要选择这个不可数的所有东西。

不只是那些属性,但我认为这可能与group by?

1 个答案:

答案 0 :(得分:1)

您可以直接创建CreditItem作为分组变量:

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