LINQ. I have an IEnumerable of type Transaction
:
private class Transaction{
public string Debitor { get; set; }
public double Debit { get; set; }
public string Creditor { get; set; } }
Sample IEnumerable<Transaction>
:
[Debitor] | [Spend] | [Creditor]
luca 10 alessio
giulia 12 alessio
alessio 7 luca
alessio 6 giulia
marco 5 giulia
alessio 3 marco
luca 1 alessio
I would like to group the Transaction
s where Debitor
== Creditor
; else in a separate group. For the previous example, I should get:
Group 1:
luca 10 alessio
alessio 7 luca
luca 1 alessio
Group 2:
giulia 12 alessio
alessio 6 giulia
Group 3:
marco 5 giulia
Group 4:
alessio 3 marco
I have solved it using 2 for loops (one nested) over the same IEnumerable, performing the check and using separate lists for the output, but I wonder if there is a less clunky way of doing this using LINQ.
A similar question might be: LINQ Conditional Group, however in this case the grouping condition is variable dependent on the other elements of the IEnumerable.