Get the latest item in the JSON array

With the below results, I want to be able to get the latest run of the queue item.
image

The results are coming from
Results = arrJSON.Cast(Of JObject).Where(Function (x) x(“QueueItemId”).toString.Equals(strQueueID)).toList

where
arrJSON is coming from a deserialised JSON array of an Orchestrator HTTP request
Results is a Generic JObject List

@redanime94 ,

Did you try Results.Last?

Or Results(Results.count-1)

If this doesn’t give latest data, then you can try Results(0) and check.

1 Like

@redanime94

You can use .OrderByDescending(function(x) Cdate(x("DateKey").ToString)).FirstOrDefault

Change datekey to the actual key of the date field

Hope this helps

Cheers

it worked but the items are not sorted in descending / ascending order

how do i use this one?

@redanime94

In the formula after the where clause add this …then the items would be sorted

arrJSON.Cast(Of JObject).Where(Function (x) x(“QueueItemId”).toString.Equals(strQueueID)).OrderByDescending(function(x) Cdate(x("DateKey").ToString)).FirstOrDefault

This gives the first item directly after sort

Cheers

Getting this error:
image

what type of variable should i use for the result please?

@redanime94

I guess your results is of type list of jobject…please change it to jobject as you will get the latest directly…

Cheers

with firstOrDefault a single JObject or null is returned. So datatype of Results = JObject
As the Variable name is expressing a plural but statement returns a single element feel free also to check the variable name naming

This topic was automatically closed 3 days after the last reply. New replies are no longer allowed.