Hi venkatg,
Check this example. Now please take its reference and correct your code.
Code
C#
protected void Page_Load(object sender, EventArgs e)
{
var students = new List<Student>()
{
new Student() { Id = 1, Name = "Bill" },
new Student() { Id = 4, Name = "Steve" },
new Student() { Id = 5, Name = "Ram" },
new Student() { Id = 4, Name = "Abdul" }
};
students = students.OrderBy(x => x.Id).ToList();
bool isInOrder = true;
int id = 0;
int newId = 0;
for (int i = 0; i < students.Count; i++)
{
id = students[i].Id;
if (i == 0)
{
newId = id + 1;
}
else
{
if (id != newId)
{
isInOrder = false;
break;
}
newId = id + 1;
}
}
if (isInOrder)
{
ClientScript.RegisterClientScriptBlock(this.GetType(), "", "alert('ID is in incremntal order.')", true);
}
else
{
ClientScript.RegisterClientScriptBlock(this.GetType(), "", "alert('ID is not in incremntal order.')", true);
}
}
public class Student
{
public int Id { get; set; }
public string Name { get; set; }
}
VB.Net
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
Dim students = New List(Of Student)() From {
New Student() With {.Id = 1, .Name = "Bill"},
New Student() With {.Id = 4, .Name = "Steve"},
New Student() With {.Id = 5, .Name = "Ram"},
New Student() With {.Id = 4, .Name = "Abdul"}
}
students = students.OrderBy(Function(x) x.Id).ToList()
Dim isInOrder As Boolean = True
Dim id As Integer = 0
Dim newId As Integer = 0
For i As Integer = 0 To students.Count - 1
id = students(i).Id
If i = 0 Then
newId = id + 1
Else
If id <> newId Then
isInOrder = False
Exit For
End If
newId = id + 1
End If
Next
If isInOrder Then
ClientScript.RegisterClientScriptBlock(Me.GetType(), "", "alert('ID is in incremntal order.')", True)
Else
ClientScript.RegisterClientScriptBlock(Me.GetType(), "", "alert('ID is not in incremntal order.')", True)
End If
End Sub
Public Class Student
Public Property Id As Integer
Public Property Name As String
End Class