My object looks something like this:
public class Transaction
{
long Id;
long Amount;
DateTime Date;
string ReferenceNumber;
string ParentReferenceNumber;
}
It is already coming in sorted by Date
, but what I need to do is arrange the list of these transactions so that those having a ParentReferenceNumber
that matches the ReferenceNumber
on another Transaction
appear in the order of "Parent then Child".
Here is what I tried. It produces the error "Collection was modified; enumeration operation may not execute." That's what I was afraid of, hence the question.
foreach (var p in Model.PaymentInfo)
{
var child = Model.PaymentInfo.SingleOrDefault(x => x.ParentReferenceNumber == p.ReferenceNumber);
if (child != null)
{
var parentIndex = Model.PaymentInfo.IndexOf(p);
var childIndex = Model.PaymentInfo.IndexOf(child);
Model.PaymentInfo.RemoveAt(childIndex);
Model.PaymentInfo.Insert(parentIndex + 1, child);
}
}