title: Merging some List<double[]> within a List<List<double[]>>
body: <p>I am struggling trying to merge a list of lists in a particular way. What I want to do is described below.</p>
  
  <p>My input lists are:</p>
  
  <p>List A = <code>List&lt;List&lt;double[]&gt;&gt;</code></p>
  
  <p>List B = <code>List&lt;int[]&gt;</code></p>
  
  <p>And the desired output is:</p>
  
  <p>List C = <code>List&lt;List&lt;double[]&gt;&gt;</code></p>
  
  <p>There are some of the <code>List&lt;double[]&gt;</code> within List A that I want to merge (e.g. using  the Union capability in linq) and store the resultant list in a <code>List&lt;List&lt;double[]&gt;&gt;</code> called "ListC".</p>
  
  <p>The indices of the Lists to be merged within ListA are stored in the ListB. If the elements are not supposed to be merged (their index doesn't appear in ListB) then we add them directly to ListC.</p>
  
  <p><strong><em>Example</em></strong>: </p>
  
  <p><strong><em>Inputs</em></strong>: </p>
  
  <pre><code>ListA = new List&lt;List&lt;double[]&gt;&gt;() { a0, a1, a2, a3, a4, a5}; 
  </code></pre>
  
  <p>where ai = <code>List&lt;double[]&gt;</code></p>
  
  <pre><code>ListB = new List&lt;int[]&gt;() { new int[]{0,1,2}, new int[]{3,5} }; 
  </code></pre>
  
  <p>indicates that the lists 0-1-2 have to be merged into one. Same with lists 3-5</p>
  
  <p><strong>Output</strong>: ListC = <code>List&lt;List&lt;double[]&gt;&gt;</code> contains all the merged <code>List&lt;double[]&gt;</code> as well as all the unique <code>List&lt;double[]&gt;</code> of List A. That is:</p>
  
  <p>--> c0= union of a0, a1, a2 "three lists to be merged into 1"</p>
  
  <p>--> c1= union of a3, a5 "two lists to be merged into 2"</p>
  
  <p>--> c2= a4 "is a unique list"</p>
  
  <p>Note: duplicates are not a problem.</p>
  
  <p>What would be a good way to achieve this?</p>
  
  <p>The code:</p>
  
  <pre><code>using System;
  using System.Collections.Generic;
  using System.Linq;
  
  
  class _unionLists
  {
  
      public static void Main(string[] args)
      {
  
          #region input lists
          //--------------------LIST A---------------------------------------------------
          //List a0
          var point1 = new double[] { 3, 3, 0 };
          var point2 = new double[] { 9, 6, 0 };
          var point3 = new double[] { 12, 8, 0 };
          var point4 = new double[] { 18, 8, 0 };
          List&lt;double[]&gt; a0 = new List&lt;double[]&gt;() { point1, point2, point3, point4 };
  
          //List a1
          var point5 = new double[] { 3, 3, 0 };
          var point6 = new double[] { 9, 7, 0 };
          var point7 = new double[] { 15, 7, 0 };
          var point8 = new double[] { 21, 15, 0 };
          List&lt;double[]&gt; a1 = new List&lt;double[]&gt;() { point5, point6, point7, point8 };
  
          //List a2
          var point9 = new double[] { 20, 13, 0 };
          var point10 = new double[] { 22, 16, 0 };
          List&lt;double[]&gt; a2 = new List&lt;double[]&gt;() { point9, point10 };
  
          //List a3
          var point11 = new double[] { 15, 19, 0 };
          var point12 = new double[] { 27, 19, 0 };
          List&lt;double[]&gt; a3 = new List&lt;double[]&gt;() { point11, point12 };
  
          //List a4
          var point13 = new double[] { 18, 20, 0 };
          var point14 = new double[] { 21, 19, 0 };
          List&lt;double[]&gt; a4 = new List&lt;double[]&gt;() { point13, point14 };
  
          //List a5
          var point15 = new double[] { 27, 19.5, 0 };
          var point16 = new double[] { 30, 5, 0 };
          List&lt;double[]&gt; a5 = new List&lt;double[]&gt;() { point15, point16 };          
  
          var ListA = new List&lt;List&lt;double[]&gt;&gt;() { a0, a1, a2, a3, a4, a5};
  
          //--------------------LIST B---------------------------------------------------
          var ListB = new List&lt;int[]&gt;() { new int[]{0,1,2} , new int[]{3,5} };
  
          #endregion
  
          //There are some of the List&lt;double[]&gt; within List A that I want to merge (e.g. using  the Union capability in linq) and store the resultant list in a List&lt;List&lt;double[]&gt;&gt; called "ListC".
          //The indices of the Lists to be merged within ListA are stored in the ListB. If the elements are not suposed to be merged (their index doesn't appear in ListB) then we add them directly to ListC.
          //
          //Example: 
          //  Inputs: ListA = new List&lt;List&lt;double[]&gt;&gt;() { a0, a1, a2, a3, a4, a5}; where ai=List&lt;double[]&gt;;
          //          ListB = new List&lt;int[]&gt;() { new int[]{0,1,2} , new int[]{3,5} }; indicates that the lists 0-1-2 have to be merged into one. Same with lists 3-5
          //  Output: ListC= List&lt;List&lt;double[]&gt;&gt; contains all the merged List&lt;double[]&gt; as well as all the unique List&lt;double[]&gt; of List A. That is:
          //          c0= union of a0, a1, a2 "three lists to be merged into 1"
          //          c1= union of a3, a5 "two lists to be merged into 2"
          //          c2= a4 "is a unique list"
  
          var ListC = new List&lt;List&lt;double[]&gt;&gt;();
  
  
      }
  }
  </code></pre>