0

我有一个列表(交易):

Type | State  
----------------
boat | buy
boat | buy
boat | sell
car  | sell
car  | sell
car  | sell
plane| buy

如何将其转换为列表(事务缩短):

Type | State | Quantity  
-----------------------
boat | buy   | 2
boat | sell  | 1
car  | sell  | 3
plane| buy|  | 1

优雅地。

4

1 回答 1

1

使用 LINQ 和Enumerable.GroupBy

Dim transactionGroups =
        From t In transactions
        Group t By Key = New With {
            .Type = t.Type,
            .State = t.State
        } Into Group
        Select New With {
            .Type = Key.Type,
            .State = Key.State,
            .Quantity = Group.Count()
        }

For Each tGroup In transactionGroups
    Dim type = tGroup.Type
    Dim state = tGroup.State
    Dim quantity = tGroup.Quantity  
Next
于 2012-06-05T14:14:09.857 回答