title: LINQ Group & Sum on a list of Models where a property is another model
body: <p>I havent been able to find any answers to this specific question on LINQ Group &amp; Aggregation so am hoping someone here can help. I have a list of models of such:</p>
  <pre><code>public class BasketProduct
  {
      public ProductItem  Product { get; set; }
      public int          Quantity { get; set; }
      public decimal      SubTotal { get; set; }
      public DateTime     DateAdded { get; set; }
  }
  </code></pre>
  <p>where the first property is another model:</p>
  <pre><code>public class ProductItem
  {
      public int      ID { get; set; }
      public string   Description { get; set; }
      public char     Item { get; set; }
      public decimal  Price { get; set; }
      public string   ImagePath { get; set; }
      public string   Barcode { get; set; }
  }
  </code></pre>
  <p>I basically want to be able to Group and Aggregate on this list:</p>
  <pre><code>List&lt;BasketProduct&gt; allBasketProducts = 
  </code></pre>
  <p>Using the following:</p>
  <pre><code>        allBasketProducts = allBasketProducts
              .GroupBy(x =&gt; x.Product.ID)
              .Select(y =&gt; new BasketProduct
                                          {
                                              Product.ID = y.First().Product.ID,
                                              Product.Item = y.First().Product.Item,
                                              Product.Description = y.First().Product.Description,
                                              Quantity = y.Sum(z =&gt; z.Quantity),
                                              Product.ImagePath = y.First().Product.ImagePath,
                                              Product.Price = y.First().Product.Price,
                                              SubTotal = y.Sum(z =&gt; z.SubTotal)
                                          }).ToList();
  </code></pre>
  <p>However it seriously doesn't like this (as per red squigly lines and even red'er text):</p>
  <p><a href="https://i.stack.imgur.com/lYsK7.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/lYsK7.png" alt="enter image description here" /></a></p>
  <p>Can someone help please?</p>