Get the Last Value of the Specific Column

Hi,

I would like to get the last value of a specific column but it is returning the whole column values.
below is my code:
LastSSN = row(1).ToString().Split(","c).Last()

here is the result:
image

I would like to only get β€œ821024241”

Your immediate assistance is greatly appreciated.

Thank you!

Hi.

There are probably a couple ways.

One way is to take the last row, then extract the column of that row.

valueDT.Rows(valueDT.Rows.Count-1)(1).ToString

Another way is to take all the items of the column as a list then extract the last item using a lambda expression

valueDT.AsEnumerable.Select(Function(r) r(1).ToString).ToArray.Last

Both methods are to be used outside of the for each since you are only extracting the last item in the column and don’t need to loop through everything.

Regards.

5 Likes

Thank you ClaytonM! your response is very fast!

this one is working:
valueDT.AsEnumerable.Select(Function(r) r(1).ToString).ToArray.Last

thanks again!

No problem.

i realized .Last cannot be used with .Rows, so the alternative was to use .Count
For example:

valueDT.Rows(valueDT.Rows.Count-1)(1).ToString

(I will correct it in my previous post.)

To be honest, I prefer the second solution I provided though or atleast knowing it cause it is very versatile and powerful.

Regards.

valueDT.Rows(valueDT.Rows.Count-1)(1).ToString

is working too!

I have converted the result to Int32 so that I can add a value. for example: 821024241 + 1

how can I do that?

You can use some vb to convert it when performing the math.

The number is a little long so you might use the Long type.
CLng(LastSSN)+1 or Convert.ToLong(LastSSN)+1
(for Int32, it’s just CInt() or Convert.ToInt32())

It’s also good practice to check your string to make sure it’s a number first.
For example.

If(IsNumeric(LastSSN), CLng(LastSSN), 0) + 1

(Assuming LastSSN is the string you extracted as the last ssn)

Regards.

1 Like

Got it! You are awesome! thank you very much!