Accessing Data with C#

Joining Data

If you have two data types, one of which (“child”) has a field reference to the other one (“parent”), you can access data in both by joining with the “parent” type when querying the “child” type.

Let’s assume you have these data types with the following fields:

Demo.Parent:

  • Id (GUID)
  • Name (String)

Demo.Child

  • Id (GUID)
  • Name (String)
  • Parent (A reference to the Demo.Parent)

When querying the Demo.Child data type, you also want to get the Demo.Parent type’s Name field value. This is where you can join the types.

To join data from two data types when querying:

  1. Connect to the data system.
  2. Get the IQueryable instance of each data type.
  3. Join the “child” type with the “parent” type on the corresponding field using LINQ.
  4. Query data by using LINQ.
using (DataConnection connection = new DataConnection())
{
   var myList = 
      from c in connection.Get<Demo.Child>()
      join p in connection.Get<Demo.Parent>()
      on c.Parent equals p.Id
      select new {ParentName = p.Name, ChildName = c.Name};
}

Listing 18: Joining data from two data types

Now you can use the list with both the “child” and “parent” data combined:

foreach (var myItem in myList)
{
   Log.LogInformation("Demo", myItem.ParentName + ": " + myItem.ChildName);
}

Listing 19: Using joined data