Hi
I have below data . Image attached. In 5 column i have value 8 in last row . I want to get date value before this value gets changed.

In my case it is 2 row i.e 16/06/2023 then value comes 7.
Thanks
Hi
I have below data . Image attached. In 5 column i have value 8 in last row . I want to get date value before this value gets changed.

In my case it is 2 row i.e 16/06/2023 then value comes 7.
Thanks
Know the answer? Post it. Somebody with the same question will find it here.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend5days leftRegister
Mohamed Azarudeen ZRank #297 · 8.5K reputationPosted Jun 18, 2023, 2:41 AM EDT
Hi ramco
you can iterate through the rows of your data structure and check the value in the fifth column. Once you encounter the value "8", you can retrieve the date value from the previous row.
Here's a sample code snippet demonstrating this logic:
DateTime? previousDate = null; for (int i = 0; i 0 && previousDate != null) { DateTime dateBeforeEight = (DateTime)previousDate; // Use the 'dateBeforeEight' value as needed break; // Exit the loop after finding the date before the value "8" } } else { // Retrieve the date value from the current row and update 'previousDate' previousDate = Convert.ToDateTime(row[0]); // Assuming the date value is in the first column (zero-based index) } }Tuhin PaulRank #7 · 71.9K reputationPosted Jun 18, 2023, 10:52 AM EDT
var dateBeforeEight = dataTable.AsEnumerable() .SkipWhile(row => Convert.ToInt32(row[4]) != 8) .Skip(1) .FirstOrDefault()?.Field(0); if (dateBeforeEight != null) { Console.WriteLine("Date before the value '8': " + dateBeforeEight.ToString()); }Tuhin PaulRank #7 · 71.9K reputationPosted Jun 18, 2023, 11:40 AM EDT
Hi Azarudeen
The code assumes that the fifth column is at index 4 and the date value is in the first column. If the structure of the data changes or is different in different scenarios, the code may not work correctly.
Tuhin PaulRank #7 · 71.9K reputationPosted Jun 18, 2023, 11:41 AM EDT
The code does not have robust error handling. For example, if the conversion of column values to integers or date/time fails, it may result in exceptions.
Prasad RaveendranRank #320 · 7.8K reputationPosted Jun 18, 2023, 11:44 AM EDT
I'm wondering, if I change the column order in the query, what would be the result of the below code?
var dateBeforeEight = dataTable.AsEnumerable() .SkipWhile(row => Convert.ToInt32(row[4]) != 8) .Skip(1) .FirstOrDefault()?.Field(0); if (dateBeforeEight != null) { Console.WriteLine("Date before the value '8': " + dateBeforeEight.ToString()); }