title: How to map a collection of objects using LINQ to SQL?
body: <p>I'm doing some tests to get a taste of object-relational mapping using LINQ to SQL and I've got stuck trying to make a many-to-many relationship work. To keep it simple, suppose I have an <strong>Order</strong> class that owns a collection of ordered <strong>Products</strong>. How can I map this product collection using LINQ to SQL?</p>
  
  <pre><code>[Table(Name = "OrderTable")]
  public class Order
  {
      [Column(IsPrimaryKey = true, IsDbGenerated = true)]
      public int ID;
  
      [Column]
      public string Customer;
  
      [Association(ThisKey = "ID", OtherKey = "ID")] // Not working
      public EntitySet&lt;Product&gt; products = new EntitySet&lt;Product&gt;();
  }
  
  [Table(Name = "ProductTable")]
  public class Product
  {
      [Column(IsPrimaryKey = true, IsDbGenerated = true)]
      public int ID;
  
      [Column]
      public string Name;
  
      [Column]
      public double Price;
  }
  </code></pre>
  
  <p>I tried using an <strong>EntitySet</strong> as a container to hold an <strong>Order</strong>'s <strong>Products</strong> but it didn't work because LINQ queries are returning <strong>Order</strong> objects with empty products collections.</p>
  
  <p>PS: From <a href="https://stackoverflow.com/questions/3026276/many-to-many-mapping-with-linq">This question</a> it seems that I need to create a third class to keep track of <strong>Order-Product</strong> relations, but I was hoping to find a way of doing it without having to create another class.</p>