title: C# Merge properties from two (or more) lists based on common property
body: <p>I have three lists of objects each of which are linked to one other list by a single common property, <code>Parent</code> and <code>Node</code> in the following hierarchy: Model -> Intermediate -> Results. I have the following code:</p>
  
  <pre><code>class Result
  {
      public string Name { get; set; }
      public int Node{ get; set; }
      public int Parent{ get; set; }
  }
  class Intermediate
  {
      public int Node{ get; set; }
      public int Parent{ get; set; }
  }
  class Model
  {
      public string Name { get; set; }
      public int Node{ get; set; }
      public int Parent{ get; set; }
  }
  
  public class Example
  {
      public static void Main()
      {
          List&lt;Result&gt; results = new List&lt;Result&gt;();
          List&lt;Intermediate&gt; intermediates = new List&lt;Intermediate&gt;();
          List&lt;Model&gt; models = new List&lt;Model&gt;();
  
          // Example objects in the list
          results.Add(new Result() { Name = "", Parent = 21, Node = 101 });
          intermediates.Add(new Part() { Parent = 11, Node = 21 });
          models.Add(new Part() { Name = "ABCD", Parent = 1, Node = 11 });
          ...
      }
  }
  </code></pre>
  
  <p>As can be seen the <code>Model</code> object links to <code>Intermediate</code> object via <code>model.Node</code> and <code>intermediate.Parent</code>, and the <code>Intermediate</code> object links to <code>Results</code> object via <code>intermediate.Node</code> and <code>results.Parent</code>.
  Note the lists can contain thousands of items, each added using a similar format as above. </p>
  
  <p>What I want to be able to do is add the names from the <code>Model</code> objects in list to the matching <code>Results</code> objects in the <code>results</code> list.</p>
  
  <p>My thinking is that I can loop through each object in the <code>Intermediate</code> list and find (using LINQ) the <code>Result</code> object where <code>intermediate.Node = result.Parent</code>, and then either replace the value of the <code>result.Parent</code> with <code>intermediate.Parent</code>, or add a new <code>Grandparent</code> property to the <code>Result</code> object in which to put the <code>intermediate.Parent</code>. Then repeat the process looping through each of the objects in the <code>models</code> list finding the matching <code>Result</code> object and adding the <code>Name</code>.</p>
  
  <p>So I guess my question is, is this the best way of doing this, or is there a more efficient way? I have many lists where the same will have to be repeated, so was wondering if there was a better way as it can be quite slow looping through every object. Also is there a way to get from the first list directly to the third list.</p>
  
  <p>I hope this is well enough explained. I am quite a beginner when it comes to C#.</p>