Loop Through Records in a view using X++

Loop through records in a View Ax 2012 R2( Need a job to achieve this)

I have a viewA where i have three fields
Destination , Source, FieldX

I have a table where i have master list for Source
This table contains S1, S2…

In the view for each S1 i need to find the final Destination Value
Destination Source Type
D1 S1
D2 D1
D2 No record found
Explanation for above example: I took S1 from master list and placed in Source field in View.
then I read Destination value for that Record which is D1. Now I placed D1 in the source field filter
and i found a record with destination value D2. Now i took D2 and i placed
it in the source field filter and i didnt find a record. i stop the loop here and i had
to save D2 value.

For S1 , the final Destination value is D2.

Example 2:

Destination Source Type
da s2
db da
dc db
dc No record found

For S2, the final Destination value is Dc.

I am not sure how i should loop in view for S1 to find the D2.
I had to do this process one by one for all S1,s2…Sn.

I tried few things using while/for or other loops with no luck.
Can someone help me with a base code to acheive this?