Although I don’t think this is the solution to your problem, this is an answer to your question.
See two examples of how to find the first gap/hole in a set of codes represented as string
, but can be converted into integers.
Using Linq
, Range()
and Except()
with only a few additional validations.
string[] codigos = { "01", "03", "05", "04" };
List<int> codigosConvertidos = codigos.Select(x => int.Parse(x)).ToList();
int intervaloEncontrado = 1;
int valorMaximo = codigosConvertidos.Max();
if (valorMaximo > 0)
intervaloEncontrado = Enumerable.Range(codigosConvertidos.Min(), codigosConvertidos.Count()).Except(codigosConvertidos).FirstOrDefault();
int primeiroCodigoDisponivel = intervaloEncontrado > 0 ? intervaloEncontrado : valorMaximo + 1;
string resultado = primeiroCodigoDisponivel.ToString().PadLeft(2, '0');
And below I leave another example using the Linq
just to sort the vector
string[] codigos = { "01", "03", "05", "04" };
var inteirosOrdenados = codigos.Select(x => int.Parse(x)).OrderBy(x => x).ToArray();
int? codigoAnterior = null;
int? intervaloEncontrado = null;
int tamanhoVetor = inteirosOrdenados.Length;
for (int i = 0; i < tamanhoVetor; i++)
{
if (codigoAnterior != null)
{
int intervaloEsperado = inteirosOrdenados[i] - 1;
if (intervaloEsperado != codigoAnterior)
{
intervaloEncontrado = intervaloEsperado;
break;
}
else
{
codigoAnterior = inteirosOrdenados[i];
}
}
else
{
codigoAnterior = inteirosOrdenados[i];
}
}
if (intervaloEncontrado == null)
intervaloEncontrado = inteirosOrdenados[tamanhoVetor - 1] + 1;
string resultado = intervaloEncontrado.ToString().PadLeft(2, '0');
Table.Firstordefault(o => o.Code == "02"); // selects the code "02" or null (if it does not exist) Table.Select(o => o.Code). Tolist() // selects all codes
– Henrique
The
Codigo
always accompanies theId
? That is, a possible Id 9 will have the code "09" or it can have an Id 9 and code "11" (for example)?– João Martins
John, yes, Id has nothing to do with the Code.
– Perci Mantovani Filho
So how can one know that there is a "fault" between the codes? The code is sequential?
– João Martins
No, the code isn’t sequential. Just what I need is this, in the table records, from the first, to find which record where there is a sequence "break". I found a solution, but the code is in SQL, I needed to "convert" to Linq, but I’m not getting it. I’ll leave it here, maybe you or another colleague can help me: Select t1.codigogroup + 1 as free FROM group AS t1 LEFT JOIN group AS t2 on t1.codigogroup + 1 = t2.codigogroup Where t2.codigogroup IS NULL AND t1.codigogroup > 1
– Perci Mantovani Filho
You will need to create a set of all the intermediate items between the smallest and largest code you have, and then make a selection of which ones do not exist in your original set, where the gaps would be.
– Diego Rafael Souza
In case, would id item 5 receive code "02"? because you need this sequence with this weak interface?
– Leandro Angelo
I think that that question is related to the same problem, but with the perspective of the bank instead of Linus
– Diego Rafael Souza