title: Dynamic Linq using Data Objects. How to convert Int32 to String for purpose of calling String.Contains()
body: <p>I am using Dynamic Linq to execute a T-SQL where clause against LINQ. This works great except when trying to convert the LIKE statement which I have manually attempted to convert with a function that I have included at the end of the post. The code is not even close to being perfect, but I stopped programming this when I realized that I would get an error during testing. The code that executes essentially takes this:</p>
  
  <p><strong>"trx_no like '%3500%'"</strong></p>
  
  <p>And converts it to this:</p>
  
  <p><strong>"trx_no.Contains("3500")"</strong></p>
  
  <p>To execute this:</p>
  
  <p><strong>Dim x = y.Where("trx_no.Contains("3500")", Nothing)</strong></p>
  
  <p>The Error:</p>
  
  <p><em><strong>No applicable method 'Contains' exists in type 'Int32?'</em></strong> </p>
  
  <p>The issue I think is that I need to convert the "trx_no" which is a Nullable(Of Int32) to a String, so after two days of research and book reading I figured that I would need to convert the string in a delegate function which I can not get to work.</p>
  
  <p>I have also tried to use Cast like in this link <a href="http://social.msdn.microsoft.com/Forums/hu-HU/adodotnetentityframework/thread/81005b38-9f02-4ebe-8995-87526ba2b700" rel="nofollow">here</a>, however this fails with an error:</p>
  
  <p><em><strong>Expression of type 'Boolean' expected</em></strong></p>
  
  <p>My version looked like this:</p>
  
  <p><strong>Dim x = y.Where("DirectCast(trx_no, System.String) like '%35000%'", Nothing)</strong></p>
  
  <p>If I have not included enough code I am sorry I just did not want to make this to overwhelming. Any suggestions would be much appreciated. Thank You.</p>
  
  <pre><code>`Private Function replaceLike(ByVal str As String) As String
      Dim rtn As String = ""
      If str.ToUpper.Contains(" LIKE '") Then
          Dim firstQuote As Int32 = str.ToUpper.IndexOf(" LIKE '") + 6
          If str.ToUpper.Chars(firstQuote + 1) = Chr(37) Then
              'If the character after the first single quote is a %, this is a Contains or EndsWith
              Dim secondQuote As Int32 = str.ToUpper.IndexOf("'", firstQuote + 1)
              If str.ToUpper.Chars(secondQuote - 1) = Chr(37) Then
                  'Handles '%%', '%value%', '%'
                  'Found % before the last quote, this is a Contains or has the value of '%'.
                  Dim val = ""
                  'See if the value is empty so that we can extract the value
                  Select Case (secondQuote - 1) - (firstQuote + 1)
                      Case 0
                          'Has no value don't add
                      Case 1
                          'Has no value don't add
                      Case Else
                          val = str.Substring(firstQuote + 2, ((secondQuote - 2) - firstQuote - 1))
                  End Select
                  str = str.Remove(firstQuote - 6, secondQuote - (firstQuote - 7))
                  str = str.Insert(firstQuote - 6, ".Contains(""" &amp; val &amp; """) ")
              Else
                  'Handles '%value'
                  'Did not find another % before the last quote, this is a EndsWith
                  Dim val = str.Substring(firstQuote + 2, ((secondQuote - 2) - firstQuote - 1))
                  str = str.Remove(firstQuote - 6, secondQuote - (firstQuote - 7))
                  str = str.Insert(firstQuote - 6, ".EndsWith(""" &amp; val &amp; """) ")
              End If
  
          Else
              'Else the character after the first single quote is not a %, this is a StartWith or is Empty
              Dim secondQuote As Int32 = str.ToUpper.IndexOf("'", firstQuote + 1)
              If str.ToUpper.Chars(secondQuote - 1) = Chr(37) Then
                  'Handles 'value%'
                  'Found a % before the last quote, this is a StartsWith
                  Dim val = str.Substring(firstQuote + 2, ((secondQuote - 2) - firstQuote - 1))
                  str = str.Remove(firstQuote - 6, secondQuote - (firstQuote - 7))
                  str = str.Insert(firstQuote - 6, ".StartsWith(""" &amp; val &amp; """) ")
              Else
                  'Handles ''
                  'Found no %
                  str = str.Remove(firstQuote - 6, secondQuote - (firstQuote - 7))
                  str = str.Insert(firstQuote - 6, ".Contains("""") ")
              End If
          End If
          rtn = replaceLike(str)
      Else
          Return str
      End If
  
      Return rtn
  End Function
  </code></pre>