@Klaus Nørregaard Thanks for using Microsoft Q&A forum and posting your query. Could you please provide more details on what your nested data structure would look like? Are you planning to create a list of relationships for each node and have a record of its child nodes associated with it? By that, I mean should the output based on your twin graph should look as follows-
- P1 -> RuleManife...
- RuleManife.. -> RuleA
- RuleA -> RuleC, RuleCC
If so, we can leverage the Query API of digital twin graph to fetch these values and get the desired result. Here is the code I have used in C# to perform the Digital Twin search and pull the results.
using System;
using Azure.DigitalTwins.Core;
using Azure.Identity;
using System.Threading.Tasks;
using System.IO;
using System.Collections.Generic;
using Azure;
using System.Text.Json;
namespace MyApp // Note: actual namespace depends on the project name.
{
internal class Program
{
static async Task Main(string[] args)
{
Console.WriteLine("Hello World!");
string adtInstanceUrl = "https://<DigitalTwinIsntanceID>.api.eus.digitaltwins.azure.net";
var credential = new DefaultAzureCredential();
var client = new DigitalTwinsClient(new Uri(adtInstanceUrl), credential);
Console.WriteLine($"Service client created – ready to go");
string twinname;
string twinQuery = "SELECT * FROM DIGITALTWINS DT WHERE IS_OF_MODEL(DT, 'dtmi:example:GraphModel;2')";
// Run a query for all twins
string queryChildren;
Dictionary<string, List<string>> childrelationship = new Dictionary<string, List<string>>();
AsyncPageable<BasicDigitalTwin> twinResult = client.QueryAsync<BasicDigitalTwin>(twinQuery);
await foreach (BasicDigitalTwin twin in twinResult)
{
Console.WriteLine(JsonSerializer.Serialize(twin.Id));
twinname = twin.Id;
queryChildren = "SELECT DT FROM DIGITALTWINS twin JOIN DT RELATED twin.contains WHERE twin.$dtId = '" + twinname + "'";
AsyncPageable<JsonElement> queryChildrenResult = client.QueryAsync<JsonElement>(queryChildren);
List<string> childnodes = new List<string>();
await foreach (JsonElement twinresult in queryChildrenResult)
{
try
{
childnodes.Add(twinresult.GetProperty("DT").GetProperty("$dtId").ToString());
}
catch (Exception e)
{
Console.WriteLine(e.Message);
}
}
childrelationship.Add(twinname, childnodes);
Console.WriteLine("---------------");
}
Console.WriteLine();
foreach (KeyValuePair<string, List<string>> kvp in childrelationship)
{
Console.WriteLine("Key = {0}, Value = {1}",
kvp.Key, String.Join(",", kvp.Value));
}
}
}
}
I have performed a search operation on the following Digital Twin graph to fetch each child node.
Here is the output I captured into a collection that records a list of child nodes for a given node.
Kindly provide more details if my understanding of your use case is incorrect and we would be glad to help you.
If the response helped, do Mark the answer as useful. All community members with similar issues will benefit by doing so. If it doesn't work, please let us know the progress and we would be glad to assist you. Your contribution is highly appreciated.