Facebook
From Bitty Rhinoceros, 3 Years ago, written in Plain Text.
Embed
Download Paste or View Raw
Hits: 58
  1.  public static int GetDictionaryMaxNr(DictionaryTypeEnum dictType, int unitId, DatabaseContext db)
  2.         {
  3.             var result = db.DictionariesValues
  4.                 .Where(x =>
  5.                 x.DictionaryTypeValue == dictType &&
  6.                 x.EntityStatus == EntityStatus.esActive
  7.                 )
  8.                 .OrderByDescending(x => x.Nr)
  9.                 .Select(x =>
  10.                 x.Nr);
  11.  
  12.             int count = result.Count();
  13.  
  14.             if (count == 0)
  15.             {
  16.                 return 1;
  17.             }
  18.  
  19.             if (result.Max() != count)
  20.             {
  21.                 //pierwszy "wolny" nr
  22.                 foreach (var nr in result)
  23.                 {
  24.                     if (count != nr)
  25.                     {
  26.                         return count;
  27.                     }
  28.                     count--;
  29.                 }
  30.             }
  31.  
  32.             return count + 1;
  33.  
  34.         }