Assuming you have a list of these items, you can use LINQ and use the ForEach extension:
List<Invoice> invoices = ; invoices .GroupBy(inv => inv.InvoiceNumber) .ForEach(group => group.Skip(1).ForEach(notFirst => notFirst.IsDupe = true));
It groups accounts up by InvoiceNumber , and if the group contains more than one element, then it sets IsDupe to true for all but the first.
However, using ForEach seems non-LINQ-compatible and less readable to me.
It looks great using ForEach :
foreach (var group in invoices.GroupBy(inv => inv.InvoiceNumber)) { foreach (var notFirstItem in group.Skip(1)) { notFirstItem.IsDupe = true; } }
Now it is absolutely readable - take each group, take all the elements except the first, and mark IsDupe = true .
Yeldar kurmangaliyev
source share