-1
I have a list of financial categories
My model
public class Categoria
{
public Guid Id {get;set;}
public string Nome {get;set;}
public int Sequencia {get;set;}
}
And as you move them up and down you need to update the Sequence property
var oldIndex = categoria.Sequencia;
var newIndex = categoria.Sequencia + 1 //Para cima
var newIndex = categoria.Sequencia - 1 //Para baixo
But I have no idea how to reorder the Sequencia field by updating its codes
Example, initial list:
1 - ÁGUA
2 - LUZ,
3 - Internet
You give a Up
of the Internet, then it should update and stay in the following sequence
1 - ÁGUA
2 - Internet
3 - Luz
What I’ve tried so far:
var categoria = await db.categorias.FirstOrDefaultAsync(x => x.Id == id);
var categorias = await db.categorias.ToListAsync();
var oldIndex = categoria.Sequencia;
var newIndex = direcao == "UP" ? categoria.Sequencia + 1 : categoria.Sequencia - 1;
foreach (var cat in categorias.Where(x => x.Sequencia > newIndex && x.Sequencia <= oldIndex && x.Id != categoria.Id))
{
cat.Sequencia = cat.Sequencia --;
}
category.Sequencia = newIndex;
I’m sorry, I couldn’t understand your doubt
– Leandro Angelo
@Leandroangelo would be to reorder the Dice when it is passed Up or Down
– Marcelo Dias
But if the user clicks 10 times in a row on the up or down, will you do this process of going into the bank 20 times (since you go twice in the bank in this current code, 2 times * 10 clicks = 20) to update the sort? Perhaps you could change the flow to order everything by clicking up or down (but not yet apply the update in the database) and when the user clicks a button to apply the ordering, only in this case you perform the modifications in the database. What do you think?
– Pedro Paulo
@Pedropaulo the list usually does not exceed 30 items, even searching every hour is with a UX/UI more accessible for the user to understand
– Marcelo Dias