title: Using LINQ's "into" keyword to group join not returning collection
body: <p>So I am fairly new to LINQ and have ran into an issue that I have not been able to find a solution for. </p>
  
  <p>I was reading through the LINQ guide on msdn when I saw an article on Group Joins. </p>
  
  <p><a href="https://docs.microsoft.com/en-us/dotnet/csharp/linq/perform-grouped-joins" rel="nofollow noreferrer">https://docs.microsoft.com/en-us/dotnet/csharp/linq/perform-grouped-joins</a></p>
  
  <p>I read through the page and thought that it was exactly what I needed to complete my query. For my query, I am calling Table A which includes an ID that I use to join with a contacts table. The ID from the contacts table is then used to join with Table C on the same ID. Table C holds a Child ID that is then used to join back with the contacts table to retrieve a list of contacts. The contacts primary ID's are used to join with a phone number table. Contacts can have multiple phone numbers.</p>
  
  <pre><code>var query = (from a in db.TableA
                       join b in db.Contacts on a.PrimaryID equals b.PrimaryID
                       join c in db.tableC on b.PrimaryID equals c.ParentPrimaryID
                       join d in db.Contacts on c.ChildID equals d.PrimaryID
                       join e in db.TablePhoneNbrs on d.PrimaryID equals e.PrimaryID into gj
                       where a.PrimaryID == id
                       select new
                       {
                           PrimaryId = d.PrimaryId,
                           FirstName = d.FirstName,
                           LastName = d.LastName,
                           phhoneNbrs = gj
                       });
  </code></pre>
  
  <p>So from the article, I read that if you join two tables and then call "into" after the join, it will store the results of the query into whatever variable you specify as a collection. So in the query above, I tried joining the individual contacts with their single/multiple phone number(s) and then calling "into" to return a collection, instead of duplicating the contact for each number, but it failed. Instead of returning a collection of phone numbers for contacts with multiple numbers, it just duplicated the contact for each number that was returned.</p>
  
  <p>So then I tried reducing the query and instead using a specific contact ID number and testing it out with just a single result.</p>
  
  <pre><code>var query2 = (from a in db.Contacts
       join b in db.TablePhoneNbrs on a.PrimaryID equalsb.PrimaryID into gj
       where a.PrimaryID == 1234
       select new
       {
           PrimaryID = a.PrimaryID,
           FirstName = a.FirstName,
           LastName = a.LastName,
           phhoneNbrs = gj
        }).ToList();
  </code></pre>
  
  <p>And this worked! So when I executed the query with a single contact ID, it correctly returned the contact and their two phone numbers as an array attached to the one contact object.</p>
  
  <p>My question is if I am doing something wrong with the first query that is causing it to fail?</p>
  
  <p><strong>UPDATE: With a much better example!</strong>
  So this is pretty much exactly how the relations work with the tables. This example appears to work, though. Query2 is the one that fails me at work. When it returns a contact, instead of storing the number in a collection and assigning it to Number variable, it duplicates the contact for each number that it has. </p>
  
  <pre><code>class Contact
  {
      public int primaryID { get; set; }
      public string name { get; set; }
  }
  
  class TableC
  {
      public int parentID { get; set; }
      public int childID { get; set; }
  }
  
  class TablePhoneNbr
  {
      public int primaryID { get; set; }
      public string phoneNbr { get; set; }
  }
  
  class TableEmail
  {
      public int primaryID { get; set; }
      public string email { get; set; }
  }
  
  class Program
  {
      static void Main(string[] args)
      {
          Contact mainContact = new Contact { primaryID = 1, name = "My Test Code" };
          Contact Jim = new Contact { primaryID = 2, name = "Jim" };
          Contact Bob = new Contact { primaryID = 3, name = "Bob" };
          Contact Ashley = new Contact { primaryID = 4, name = "Ashley" };
          Contact Mary = new Contact { primaryID = 5, name = "Mary" };
  
          TableC child1 = new TableC { parentID = 1, childID = 2 };
          TableC child2 = new TableC { parentID = 1, childID = 3 };
          TableC child3 = new TableC { parentID = 1, childID = 4 };
          TableC child4 = new TableC { parentID = 1, childID = 5 };
  
          TablePhoneNbr Nbr1 = new TablePhoneNbr { primaryID = 2, phoneNbr = "123456" };
          TablePhoneNbr Nbr2 = new TablePhoneNbr { primaryID = 2, phoneNbr = "999999" };
          TablePhoneNbr Nbr3 = new TablePhoneNbr { primaryID = 3, phoneNbr = "888888" };
          TablePhoneNbr Nbr4 = new TablePhoneNbr { primaryID = 4, phoneNbr = "777777" };
  
          TableEmail Email1 = new TableEmail { primaryID = 2, email = "Hello World" };
          TableEmail Email2 = new TableEmail { primaryID = 3, email = "Goodbye World" };
          TableEmail Email3 = new TableEmail { primaryID = 4, email = "Testing1" };
          TableEmail Email4 = new TableEmail { primaryID = 5, email = "Testing2" };
  
          // Create two lists.
          List&lt;Contact&gt; contacts = new List&lt;Contact&gt; { mainContact, Jim, Bob, Ashley, Mary };
          List&lt;TableC&gt; relations = new List&lt;TableC&gt; { child1, child2, child3, child4 };
          List&lt;TablePhoneNbr&gt; numbers = new List&lt;TablePhoneNbr&gt; { Nbr1, Nbr2, Nbr3, Nbr4 };
          List&lt;TableEmail&gt; emails = new List&lt;TableEmail&gt; { Email1, Email2, Email3, Email4 };
  
          // Create a list where each element is an anonymous type
          // that contains the person's first name and a collection of 
          // pets that are owned by them.
          var query = from primary in contacts
                      join relation in relations on primary.primaryID equals relation.parentID
                      join person in contacts on relation.childID equals person.primaryID
                      join number in numbers on person.primaryID equals number.primaryID into gj
                      select new { FirstName = person.name, Number = gj };
  
          foreach (var v in query)
          {
              // Output the owner's name.
              Console.WriteLine("{0}:", v.FirstName);
              // Output each of the owner's pet's names.
              foreach (TablePhoneNbr number in v.Number)
                  Console.WriteLine("  {0}", number.phoneNbr);
          }
  
          var query2 = from primary in contacts
                      join relation in relations on primary.primaryID equals relation.parentID
                      join person in contacts on relation.childID equals person.primaryID
                      join email in emails on person.primaryID equals email.primaryID
                      join number in numbers on person.primaryID equals number.primaryID into gj
                      select new { FirstName = person.name, Number = gj, Email = email.email };
  
          foreach (var v in query2)
          {
              // Output the owner's name.
              Console.WriteLine("{0}:", v.FirstName);
              Console.WriteLine("{0}:", v.Email);
              // Output each of the owner's pet's names.
              foreach (TablePhoneNbr number in v.Number)
                  Console.WriteLine("  {0}", number.phoneNbr);
          }
          Console.ReadLine();
      }
  }
  </code></pre>