How to check the missing element in the sequence
I am doing a project related to payroll where i will have some payperiodnumbers
for each and every payroll that has been runned. I will show all the payrolls in a grid view with the corresponding pay period numbers.
Assume i get the following results when i binded to grid
Now from the grid if i select 1 and click on delete i would like to show an error message stating you have to delete max pay period first.
Like that if i had my max pay period number as 7
and if user selects 1,2,3,4,5,6
and try to delete i would like to display the same error. I am saving the selected ID's
in a a开发者_C百科rraylist so can any one help me how can i check for my condition as specified. I can get the maximum payperiodid using the query but the remaining code i would like to do.
I am using 2.0
so no point of using LINQ
here. Can any one help me
As Azodious
pointed i am showing some condition that should work and some not
If max number is 7
and if i select 1,5,7
i would like to display an error message.
If i select 5,6,7
then it should delete that.
Something like this:
selectedNumbers.Sort();
selectedNumbers.Reverse();
int maxPeriodNumber = 5; // This you know
int lastValue = (int)selectedNumbers[0];
if (lastValue < maxPeriodNumber)
{
// Highest selected number is smaller than required, warn user or throw exception
return;
}
foreach (int val in selectedNumbers)
{
if (val < (lastValue - 1))
{
// There is a gap in the numbering, warn user or throw exception
return;
}
lastValue = val;
}
// When you end up here, everything is ok and you can delete the items whose numbers are in the list
A simple logic to display missing numbers
ArrayList a = new ArrayList();
List<int> lst = new List<int>();
lst.Add(1);
lst.Add(3);
lst.Add(5);
int fst = (int)lst[0];
int last = 0;
for (int i = 0; i < lst.Count; i++)
{
last = (int)lst[i];
}
for (int k = fst; k <= last; k++)
{
if (k == fst | k == last)
{
}
else
{
a.Add(k);
a.Add(" ");
}
}
Label1.Text = "Missing Numbers are" + " " + System.String.Concat(a.ToArray());
精彩评论