Accessing ADT from .NET, I am trying to obtain the number of a particular relationship each of my nodes has. For example, there are a number of people nodes and each person could have 1, 2 or more friends (a relationship of type friend). I want to retrieve a list of the node name (e.g. John) and the number of friends they have, e.g.:
John, 3 Harry, 2 Frank, 4
However, when you use JOIN in ADT, it seems you have to combine it with WHERE and use a single $dtId. So for example,
SELECT COUNT() FROM DIGITALTWINS twin JOIN parent RELATED twin.Friend WHERE twin.$dtId= 'John'
Only retrives the number of Friend relationships for this one node.
From the documentation, it seems it is possible to retrieve more than one node when using JOIN by using keyword IN e.g. (copying from docs)
SELECT Room
FROM DIGITALTWINS Floor
JOIN Room RELATED Floor.contains
WHERE Floor.$dtId IN ['floor1','floor2', ..'floorn']
AND Room. Temperature > 72
AND IS_OF_MODEL(Room, 'dtmi:com:contoso:Room;1')
However, when I apply this with variations such as:
SELECT Count() AS what FROM DIGITALTWINS twin JOIN parent RELATED twin.HasDocument WHERE twin.$dtId IN ['John', 'Harry']
I don't get what I'm looking for.
Ultimately, in c#. I would like to loop through the results with something like: await foreach (BasicDigitalTwin twin in queryResult) {}, saving to a List (where node contains nodename and numberoffriends properties.
Any help much appreciated. Maybe I need something entirely different, not a join.
The
INoperator would return the aggregate of all the relationships of the Azure Twin Id's specified in the clause. The SQL implementation of the use case you are looking for would need the use ofGROUP BYoperator. I could not find a similar approach in querying Azure Twin graphs.However, you can loop the Twin nodes and get the relationships per node using C#. I have pushed the values to a Dictionary as it made more sense to have a key value pair. You can modify the code to add it to a list as well. Here is the code I have used to achieve this.
}
You have to modify the parameters
twinQueryandqueryin the above code to get the twins from the corresponding model and the count based on the appropriate relationship.Please find the below model graph I have created on the Azure Digital Twin Explorer.
Here is the output I get from the above code for the relationship count of all the nodes.
Hope this helps!