I have two List<AccountBalance>
And AccountBalance has an ID GUID
and a Balance DECIMAL
value.
One list has all credits, and one list has all debits. These have been summed, so there will be unique account IDs in the lists (but will repeat in one another. i.e. An account will appear ONCE in creditBalances, but will probably be found in both lists.
var creditBalances = new List<AccountBalance>();
var debiBalances = new List<AccountBalance>();
What I need to do is return one list, with the total for each account.
So, I need to kind of do a join, based on the ID of the two tables... And for each account, add amount in the credit, and subtract the amount in the debit.
Not all accounts will have a credit, and not all will have a debit, but my result set needs to include all the accounts.
What I am trying:
var result = creditBalances
.Join(debitBalances, a => a.ExternalId, b => b.ExternalId, (a, b) => new AccountBalance { Id = a.ExternalId, Balance = a.amount - b.amount })
.ToList();
But this seems to do an INNER join, and only give me results where 'creditBalances' has a value. I need to basically do a CROSS JOIN?
Credit:
Acc: 1, amount 10
Acc: 2, amount 5
Debit:
Acc 2: amount 2
Acc 3: amount 5
Should result in:
Acc 1: 10
Acc 2: 3
Acc 3: -5