0

私は(トランザクションの)リストを持っています:

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

どうすればそれを(transactionShortenedの)リストに変換できますか?

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 に答える