Я хочу сравнить два списка, оба из которых могут быть нулевыми, оба могут содержать 0
или более записей.
Если сумма совпадает, должна быть некоторая обработка для этого.
Если нет, следует проверить, покрывается ли разница суммы указанным допуском.
Вот что я сделал. Есть ли более элегантный способ сделать это?
int tolerableDifference = 5; //example.
Result success = Result.Valid;
if (listA.Count == listB.Count)
{
// do whatever is to be done when counts match.
}
else
{
// Lists have different length. No match.
var absDifference = Math.Abs(listA.Count - listB.Count);
if ((listA.Count - listB.Count) > 0)
{
if (absDifference < tolerableDifference)
{
Console.WriteLine($"Difference below Tolerance threshold. Difference: {absDifference}.");
}
else
{
//Outside tolerance, too less items in listB
success = Result.Invalid | Result.TooFewItems;
}
}
else if ((listA.Count - listB.Count) < 0)
{
if (absDifference < tolerableDifference)
{
Console.WriteLine($"Difference below Tolerance threshold. Difference: {absDifference}.");
}
else
{
//Outside tolerance, too many items in listB
success = Result.Invalid | Result.TooManyItems;
}
}
}