title: Problem calling string manipulation method within linq-to-sql query
body: <p>I'm having a frustrating issue trying to use LINQ to call a string manipulation method.  I've done lots of searching now and have tried various method to get the line noted as 'FAILS' below to work.  It currently throws an exception.</p>
  
  <p>Some things I've tried:</p>
  
  <p>a) Initially the creation of the concatenated key was in the same query, didn't change anything</p>
  
  <p>b) Converting the non-string fields to strings (another whole can of works with .ToString not working in linq.  String.Concat and String.Format were tried, work ok in some cases but not when you try to refer to that value later on)</p>
  
  <p>c) Using the concat etc instead of the '+' to join the things together.</p>
  
  <p>As you can see it seems fairly tolerant of appending strings to non-strings, but <em>not</em> when that method is invoked.</p>
  
  <p>There are lots of rows so I'd prefer not to convert the data to a list/array etc, but if that's the only option then any suggestions appreciated.</p>
  
  <p>Many thanks! - Mark</p>
  
  <pre><code>    var vouchers = from v in db.Vouchers
                     select new
                     {
                         v.Amount,
                         v.Due_Date,
                         v.Invoice_Date,
                         v.PO_CC,
                         v.Vendor_No_,
                         v.Invoice_No_,
                         invoiceNumeric = MFUtil.StripNonNumeric(v.Invoice_No_)
                     };
  
  
      var keyedvouchers = from vv in vouchers
                          select new
                          {
                              thekey = vv.Vendor_No_ + "Test", // works with normal string
                              thekey2 = vv.Amount + "Test", // works with decimal
                              thekey3 = vv.Invoice_Date + "Test", // works with date
                              thekey4 = vv.invoiceNumeric, // works
                              thekey5 = vv.invoiceNumeric + "Test" // FAILS
                          };
  </code></pre>
  
  <p>-- The method to strip chars ---</p>
  
  <pre><code>   public static string StripNonNumeric(string str) 
     {
          StringBuilder sb = new StringBuilder();
          foreach (char c in str) 
          {
              // only append if its withing the acceptable boundaries
              // strip special chars: if ((c &gt;= '0' &amp;&amp; c &lt;= '9') || (c &gt;= 'A' &amp;&amp; c &lt;= 'Z') | || (c &gt;= 'a' &amp;&amp; c &lt;= 'z') | c == '.' || c == '_') 
  
              // strip any nonnumeric chars
              if (c &gt;= '0' &amp;&amp; c &lt;= '9')
                  {
                  sb.Append(c);
                  }
          }
          return sb.ToString();
      }
  </code></pre>
  
  <p>-- The Exception Message--</p>
  
  <p>System.InvalidOperationException was unhandled by user code
    Message=Could not translate expression 'Table(Voucher).Select(v => new &lt;>f__AnonymousType0<code>7(Amount = v.Amount, Due_Date = v.Due_Date, Invoice_Date = v.Invoice_Date, PO_CC = v.PO_CC, Vendor_No_ = v.Vendor_No_, Invoice_No_ = v.Invoice_No_, invoiceNumeric = StripNonNumeric(v.Invoice_No_))).Select(vv =&gt; new &lt;&gt;f__AnonymousType1</code>5(thekey = (vv.Vendor_No_ + "Test"), thekey2 = (Convert(vv.Amount) + "Test"), thekey3 = (Convert(vv.Invoice_Date) + "Test"), thekey4 = vv.invoiceNumeric, thekey5 = (vv.invoiceNumeric + "Test")))' into SQL and could not treat it as a local expression.</p>