title: How to sum a custom object with LinQ?
body: <p>First I followed this tutorial to create my Money object: <a href="https://www.codeproject.com/articles/837791/money-pattern" rel="nofollow noreferrer">https://www.codeproject.com/articles/837791/money-pattern</a></p>
  
  <pre><code>Money totalItems = _invoice.InvoiceDetails
      .Sum(y =&gt; y.Amount); // Amount is of type Money
  </code></pre>
  
  <p>I get an compilation exception on <code>y.Amount</code>:</p>
  
  <blockquote>
    <p>Cannot implicitly convert type
    'Money' to 'long?'  Cannot
    convert lambda expression to intended delegate type because some of
    the return types in the block are not implicitly convertible to the
    delegate return type</p>
  </blockquote>
  
  <p>What am I doing wrong?</p>
  
  <p>Here is my Money class:</p>
  
  <pre><code>public class Money
  {
      public decimal Amount { get; private set; }
      public CurrencyCode Currency { get; private set; }
  
      #region Constructors
      public Money() { }
      public Money(Money amount)
      {
          this.Amount = amount.Amount;
          this.Currency = amount.Currency;
      }
      public Money(decimal amount, CurrencyCode currencyCode)
      {
          this.Amount = amount;
          this.Currency = currencyCode;
      }
      public Money(int amount, CurrencyCode currency)
          : this(Convert.ToDecimal(amount), currency)
      {
      }
      public Money(double amount, CurrencyCode currency)
          : this(Convert.ToDecimal(amount), currency)
      {
      }
      #endregion
  
      #region Comprasion operators
      public static bool operator ==(Money var1, Money var2)
      {
          if ((object)var1 == null || (object)var2 == null)
              return false;
  
          if (var1.Currency != var2.Currency) return false;
          return var1.Amount == var2.Amount;
      }
  
  
      public static bool operator !=(Money var1, Money var2)
      {
          return !(var1 == var2);
      }
  
      public static bool operator &gt;(Money var1, Money var2)
      {
          if (var1.Currency != var2.Currency)
              throw new InvalidOperationException("Comprasion between different currencies is not allowed.");
  
          return var1.Amount &gt; var2.Amount;
      }
  
      public static bool operator &lt;(Money var1, Money var2)
      {
          if (var1 == var2) return false;
  
          return !(var1 &gt; var2);
      }
  
      public static bool operator &lt;=(Money var1, Money var2)
      {
          if (var1 &lt; var2 || var1 == var2) return true;
  
          return false;
      }
  
      public static bool operator &gt;=(Money var1, Money var2)
      {
          if (var1 &gt; var2 || var1 == var2) return true;
  
          return false;
      }
      #endregion
  
      #region Ariphmetical operations
      public static Money operator +(Money var1, Money var2)
      {
          if (var1.Currency != var2.Currency)
          {
              throw new InvalidCastException("Calculation is using different currencies!");
          }
  
          return new Money(var1.Amount + var2.Amount, var1.Currency);
      }
  
      public static Money operator -(Money var1, Money var2)
      {
          if (var1.Currency != var2.Currency)
          {
              throw new InvalidCastException("Calculation is using different currencies!");
          }
  
          return new Money(var1.Amount - var2.Amount, var1.Currency);
      }
  
      public static Money operator *(Money var1, Money var2)
      {
          if (var1.Currency != var2.Currency)
          {
              throw new InvalidCastException("Calculation is using different currencies!");
          }
  
          return new Money(var1.Amount * var2.Amount, var1.Currency);
      }
  
      public static Money operator /(Money var1, Money var2)
      {
          if (var1.Currency != var2.Currency)
          {
              throw new InvalidCastException("Calculation is using different currencies!");
          }
  
          return new Money(var1.Amount / var2.Amount, var1.Currency);
      }
  
      public static Money operator *(decimal var1, Money var2)
      {
          return new Money(var1 * var2.Amount, var2.Currency);
      }
  
      public static Money operator *(Money var1, decimal var2)
      {
          return new Money(var1.Amount * var2, var1.Currency);
      }
  
      public static Money operator /(decimal var1, Money var2)
      {
          return new Money(var1 / var2.Amount, var2.Currency);
      }
  
      public static Money operator /(Money var1, decimal var2)
      {
          return new Money(var1.Amount / var2, var1.Currency);
      }
  
      public static Money operator *(int var1, Money var2)
      {
          return new Money(var1 * var2.Amount, var2.Currency);
      }
  
      public static Money operator *(Money var1, int var2)
      {
          return new Money(var1.Amount * var2, var1.Currency);
      }
  
      public static Money operator /(int var1, Money var2)
      {
          return new Money(var1 / var2.Amount, var2.Currency);
      }
  
      public static Money operator /(Money var1, int var2)
      {
          return new Money(var1.Amount / var2, var1.Currency);
      }
  
      public static Money operator *(long var1, Money var2)
      {
          return new Money(var1 * var2.Amount, var2.Currency);
      }
  
      public static Money operator *(Money var1, long var2)
      {
          return new Money(var1.Amount * var2, var1.Currency);
      }
  
      public static Money operator /(long var1, Money var2)
      {
          return new Money(var1 / var2.Amount, var2.Currency);
      }
  
      public static Money operator /(Money var1, long var2)
      {
          return new Money(var1.Amount / var2, var1.Currency);
      }
      #endregion
  
      public override bool Equals(object obj)
      {
          if (obj == null) return false;
  
          Money money = obj as Money;
          return (this.Amount == money.Amount &amp;&amp; this.Currency == money.Currency);
      }
  
      public bool Equals(Money money)
      {
          if ((object)money == null) return false;
  
          return (this.Amount == money.Amount &amp;&amp; this.Currency == money.Currency);
      }
  
      public override int GetHashCode()
      {
          return base.GetHashCode();
      }
  
      public override string ToString()
      {
          return this.Amount.ToString();
      }
      #endregion
  </code></pre>