sirFunkenstine
sirFunkenstine

Reputation: 8515

linq validation issue

I have a database with a field value going from 1 to 10. When entering a value into this database the entries must be in order from 1 to 8, You can add 9 or 10 whenever you see fit. I had a Linq statement to grab the last value in the database that was less then 9 and test it against the value being entered. However it does not seem to be working when 10 is the last value in my database.

List<Row> list = new List<Row>();
list.AddRange(_db.Rows.ToList());

 // list = { "1", "2", "8","7","8","9","10" }
Row row = (
             from p in list
             where p.ProcessStage.CompareTo("9") < 0
             select p)
             .Last();

Im looking to return the last 8 value. Currently this statement is returning 10, and i have no idea why. Any help would be much appreciated. Thanks :)

Solution

I should have been converting to an int before checking.

 Row row = (
             from p in list
             where Convert.ToInt32(p.ProcessStage) < 9
             select p)
             .Last();

Upvotes: 0

Views: 59

Answers (1)

Sergio Tulentsev
Sergio Tulentsev

Reputation: 230561

This probably has something to do with lexicographical ordering. Number 10 is greater than number 9, but string "10" is less than string "9".

I guess, table column has the wrong type.

Upvotes: 2

Related Questions