c# - How to write a function that generates ID by taking missing items in a sequence? -


how can write algorithm can take unused id's out of sequence starting 1 99 in format "c00"? example newid(['c01', 'c02', 'c03']) should emit 'c04', newid(['c02', 'c03', 'c04']) should emit c01, , newid(['c01', 'c03', 'c04']) should result in c02.

i wrote implementation result wrong.

example : cat_id : c01, c02, c05, c06, c11. when run it, expected result c03. algorithm follows:

  1. sort id asc
  2. go through every item in list
  3. compare first value next, if not same, add 1 , exit loop.

this code:

 public static string get_areaid_auto()  {     string result = "";     if (db.tests.tolist().count <= 0)     {         result = "01";     }     else     {         int maxid = 0;         foreach (var item in db.tests.orderby(e => e.cat_id).tolist())         {             if (int.parse(item.cat_id.substring(1)) + 1 != int.parse(item.cat_id.substring(1)))             {                 maxid = int.parse(item.cat_id.substring(1) + 1);                 break;             }         }         switch (maxid.tostring().length)         {             case 1:                 if (maxid == 9)                 {                     result = "10";                 }                 else                     result = "0" + (maxid + 1);                 break;             case 2:                 result = "" + (maxid + 1);                 break;             default:                 break;         }     }     return "c" + result; } 

can point out wrong?

this should work you:

public static string get_areaid_auto() {     var existing = db.tests.select(e => e.cat_id).orderby(x => x).tolist();     if (existing.count == 0)     {         return "c01";     }     else     {         return             existing                 .concat(new [] { "" })                 .select((x, n) => new                 {                     actual = x,                     expected = string.format("c{0:00}", n + 1),                 })                 .where(x => x.actual != x.expected)                 .select(x => x.expected)                 .first();      } } 

this uses generate , test approach. no parsing necessary.

i realised .concat(new [] { "" }) change if statement no longer required. can instead:

public static string get_areaid_auto() {     return         db.tests             .select(e => e.cat_id)             .orderby(x => x)             .toarray()             .concat(new [] { "" })             .select((x, n) => new             {                 actual = x,                 expected = string.format("c{0:00}", n + 1),             })             .where(x => x.actual != x.expected)             .select(x => x.expected)             .first(); } 

Comments

Popular posts from this blog

java - UnknownEntityTypeException: Unable to locate persister (Hibernate 5.0) -

python - ValueError: empty vocabulary; perhaps the documents only contain stop words -

ubuntu - collect2: fatal error: ld terminated with signal 9 [Killed] -