I am currently using a normal looping to check if the list of numbers are in order.
I am currently learning LINQ and I want to know how can I implement in LINQ to check if the sequence of numbers are in correct order.
For example I have this list of sequence number:
1.0
1.1
1.2
1.4
2.0
The program needs to flag as error the line 1.4 because 1.3 is missing.
How can I achieve that using LINQ?
Thanks for all your help. 🙂
It’s like table of contents:
1.1 followed by 1.3 is invalid, 1 followed by 2 is valid. 1.4 followed by 2 is valid.
Here’s the code that I am using it still has a lot of lapses I think:
using (System.IO.StreamReader reader = new System.IO.StreamReader("D:\\test.txt"))
{
double prevNumber = 0;
while (reader.Peek() >= 0)
{
double curNumber = double.Parse(reader.ReadLine());
double x = Math.Round(curNumber - prevNumber, 1);
if (x == 0.1)
{
prevNumber = curNumber;
}
else
{
int prev = (int)Math.Floor(prevNumber);
int cur = (int)Math.Floor(curNumber);
if ((cur - prev) == 1)
{
prevNumber = curNumber;
}
else
{
//error found
}
}
}
}
This method takes a filename and returns an array of line numbers of incorrect versions. For your example it returns
{ 4 }.It only handles numbers of the form
x.y, as that appears to be all you want it to handle.Honestly? I think you’re better off with a loop.