Scenario: Database has table with list of account numbers. Account numbers range from 0-9999. Customer is allowed to make account numbers for customers within that range, as they see fit.
Need: I am producing a report that shows unused account numbers in range format. So, I need a list of strings, showing available account numbers, in range format.
Example: Account numbers 0, 1, 2, 4, 20, 21, 22 are all being used in data. So the result list would be...
3
5-19
23-9999
Been stumbling around on this all day. How to do this with straight-up c#?
Use Range and Except
var acctNos = new List<int>() { 0,1,2,4,20,21,22 };
var unusedAcctNos = Enumerable.Range(0,9999).ToList().Except(acctNos);
Then to group contiguous integers, modify the accepted solution given here.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With