Group by and Select with Models
<p>I have a list of an object defined by a model, and I want to group by a specific field. When grouped, some fields will need to be summed together. I want to group by ItemName, and the Amount will be all summed together. </p>

<p>My model is:</p>

<pre><code>public class Item{
    public string ItemName { get; set; }
    public string ItemColor { get; set; }
    public string SomethingElse { get; set; }
    public decimal Amount { get; set; }
}
</code></pre>

<p>My current attempt is: </p>

<pre><code>List&lt;Item&gt; fullListOfItems = GetItemsFromDatabase();

List&lt;Item&gt; groupedList = fullListOfItems
                         .GroupBy( l =&gt; l.ItemName )
                         .Select(i =&gt;
                             new Item() 
                             {
                                 ItemName = i.Key.ItemName,
                                 ItemColor = i.Key.ItemColor,
                                 SomethingElse = i.Key.SomethingElse,
                                 Amount = i.Sum(k=&gt;k.Amount)
                             }
                         );
</code></pre>

<p>I'm getting an error after each key statement, saying that the grouping model does not contain a defintion for ItemName. </p>

<p>Any help would be appreciated. Thanks.</p>