Reputation: 24395
I have a query that's returning some grouped data, and I'm drawing a blank on how to "flatten" it so that top level information is available at each group.
Account A
- Group A
- Group B
- Group C
- etc
Account B
- Group B
- Group C
What I want to do is "flatten" this to:
Account A | Group A | Sum(Group)
Account A | Group B | Sum(Group)
Account A | Group C | Sum(Group)
Account B | Group B | Sum(Group)
Account B | Group C | Sum(Group)
Here's what I have so far, but I'm not totally sure where to go
var transactions = Accounts.Where(clause)
.Select(x => new
{
Account = x,
TransGroupedByBucket = x.TransactionLogs.GroupBy(tl => tl.TransactionType.BucketTypeID)
})
//pseudocode
//Select new {
// Account,
// One_TransactionLog_Group
//}
//will run for each group:
.Select(x => new
{
AccountInfo = x.Account.ID
Sum = One_TransactionLog_Group.Sum(tl => tl.Amount)
})
;
I think I'm just having a brain fart, can someone point me in the right direction? One idea I had was to flip it to start at the TransactionLog
level and traverse to the Account
, but there might be 0 TransactionLog
s for an Account
, so that might not work properly.
Upvotes: 0
Views: 4367
Reputation: 276306
You could use SelectMany
:
Accounts.SelectMany(x => x.TransactionLogs.
Select(y => new { Account = x.ID,
TransactionLog = y,
Sum = ... });
Upvotes: 3