I have one ObservableCollection
with items of the following class:
public partial class consultaSelect
{
private string scriptField;
private string nombreAccionField;
private string iconoField;
private string ordenField;
private string tooltipField;
private string valueField;
//Todos con sus getters y setter respectivos.
}
The property that interests me here is orden
. It is used to sort this collection . The problem is that it orden
can have holes, that is, there can be something like that 1,2,5,7,9
saved as the deserializer thinks of it (it is a class that is loaded from an XML ).
The issue is that through the view, the user can change the orden
one of two items, and for that what he does is move it in the view (all that works and is solved). The problem comes, because if the user wants to move one position "up" , the item 5
should become 2
, and the 2
would become 5
.
The question is, how do I elegantly get what is the maximum item that is less than a certain amount?
It can be done with an while
old fashioned, what I'm looking for is a modern Linq solution that returns that.
A Linq solution could be the following, assuming you have the pivot element and the property to search for is `orderField:
Having as input the data:
1
,2
,5
,7
,9
:The output would be:
2
.Another way to solve it is with:
Here you can see thedemostración y el resultado