title: Why does linq-2-sql create extra unnecessary objects?
body: <p>I have a simple Parent Child table in a database like so</p>
  
  <pre><code>CREATE TABLE [Parent](
      [Id] [int] IDENTITY(1,1) NOT NULL,
      [Name] [nvarchar](256) NOT NULL)    
  ALTER TABLE [Parent] ADD CONSTRAINT [PK_Parent_Id] PRIMARY KEY ([Id])    
  
  CREATE TABLE [Child](
      [Id] [int] IDENTITY(1,1) NOT NULL,
      [ParentId] [int] NOT NULL,
      [Name] [nvarchar](256) NOT NULL)    
  ALTER TABLE [Child] ADD CONSTRAINT [PK_Child_Id] PRIMARY KEY ([Id])
  ALTER TABLE [Child] ADD CONSTRAINT [FK_Child_Parent_ID] 
      FOREIGN KEY([ParentId]) REFERENCES [Parent] ([Id])
  </code></pre>
  
  <p>The data that I have in them is</p>
  
  <p><strong>Parent Table</strong></p>
  
  <pre><code>Id  Name
  1   John
  </code></pre>
  
  <p><strong>Child Table</strong></p>
  
  <pre><code>Id ParentId  Name
  1     1    Mike
  2     1    Jake
  3     1    Sue
  4     1    Liz
  </code></pre>
  
  <p>These tables are mapped to <code>Parent</code> and <code>Child</code> C# objects using the Linq-2-SQL designer in Visual Studio with no non standard options.</p>
  
  <p>I made a simple test program to query all child with their parents</p>
  
  <pre><code>public partial class Parent
  {
      static int counter = 0;
      //default OnCreated created by the linq to sql designer
      partial void OnCreated()
      {
          Console.WriteLine(string.Format("CreatedParent {0} hashcode={1}",
              ++counter , GetHashCode()));
      }
  }
  
  class Program
  {
      static void Main(string[] args)
      {
          using (var db = new SimpleDbDataContext())
          {
              DataLoadOptions opts = new DataLoadOptions();
              opts.LoadWith&lt;Child&gt;(c =&gt; c.Parent);
              db.LoadOptions = opts;
              var allChildren = db.Childs.ToArray();
              foreach (var child in allChildren)
              {
                  Console.WriteLine(string.Format("Parent name={0} hashcode={1}",
                      child.Parent.Name, child.Parent.GetHashCode()));
  
              }
          }
      }
  }
  </code></pre>
  
  <p>The output of the above program is</p>
  
  <pre><code>CreatedParent 1 hashcode=53937671
  CreatedParent 2 hashcode=9874138
  CreatedParent 3 hashcode=2186493
  CreatedParent 4 hashcode=22537358
  Parent name=John hashcode=53937671
  Parent name=John hashcode=53937671
  Parent name=John hashcode=53937671
  Parent name=John hashcode=53937671
  </code></pre>
  
  <p>As you can see a <code>Parent</code> object was created for every <code>Child</code> in the database only to be discarded eventually. </p>
  
  <p>Questions:</p>
  
  <ol>
  <li>Why does Linq-2-Sql create these unnecessary extra <code>Parent</code> objects ?</li>
  <li>Are there any options to avoid creation of extra <code>Parent</code> objects ?</li>
  </ol>