Working in C# with a custom object that has multiple rows and columns.
I need to remove duplicate records where some (but not all) of the column values match, including positive and negative values of the same number.
This is to identify orders that have been charged and reversed. So the data would be like so:
CustomerId OrderNo OrderDt Qty ProductNo
1234123 6098 10/17/2025 2 32166
1234123 6098 10/17/2025 -2 32166
1234123 6098 10/17/2025 2 32166
9187324 6457 10/16/2025 10 97846
2087930 9875 10/11/2025 5 65655
2093483 3165 10/13/2025 3 89645
2093483 3165 10/13/2025 -3 89645
9784652 9784 10/14/2025 6 13246
And what I want the final object to contain would be:
CustomerId OrderNo OrderDt Qty ProductNo
1234123 6098 10/17/2025 2 32166
9187324 6457 10/16/2025 10 97846
2087930 9875 10/11/2025 5 65655
9784652 9784 10/14/2025 6 13246
(There are other fields not relevant to duplicate status - address, phone #, etc.)
Where the four values are identical and one quantity is a positive and one a negative of the same value, I want both rows removed.
If the item is charged, reversed, and then charged again I want to keep one row - rows will only be removed in sets of 2 matches.
I'm grouping and selecting by the customer #, order #, order date, and qty (thanks to Jon Skeet). I assume I need to make a copy of the object for comparison, but I'm at a loss as to how to remove both rows instead of just the one.
var customerId = orderData.FindField("CustomerID");
var orderNo = orderData.FindField("OrderNo");
var orderDate = orderData.FindField("OrderDt");
var qty = orderData.FindField("Qty");
var distinctOrders = orderData.Rows
.GroupBy(x => new
{ CustomerID = x.GetDataById(customerId.Id).ToString(),
OrderNo = x.GetDataById(orderNo.Id).ToString(),
OrderDt = x.GetDataById(orderDate.Id).ToString(),
Qty = Math.Abs(decimal.Parse(x.GetDataById(qty.Id).ToString()))
})
.Select(o => o.Select(t => new
{ CustomerID = t.GetDataById(customerId.Id).ToString(),
OrderNo = t.GetDataById(orderNo.Id).ToString(),
OrderDt = t.GetDataById(orderDate.Id).ToString(),
Qty = Math.Abs(decimal.Parse(x.GetDataById(qty.Id).ToString()))
})
.FirstOrDefault());
foreach (var distinctOrder in distinctOrders)
{
OrderData duplicateOrderData = orderData;
}

Math.Absfor the value where you want positive and negative values to be treated the same? (It's unclear why you're converting everything into strings, mind you...)2 - 2 + 2 == 2; 3 - 3 == 0)? And then, and if computedquantityis0remove such record(s)?